<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\NewsCategory;
use App\Supports\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Helper\Helper;

/**
 * @group Lottery Prize
 */
class NewsController extends Controller
{
    public function __construct(
    )
    {
    }

    public function list(Request $request, $locale = 'tw')
    {
        $locale = $locale == "tw" ? "zh_TW" : $locale;
        $request_ordering = $request->ordering ?? "desc";
        $result = [];

        //年份清單

        //置頂影音
        $result["top"] = [];
        $top = News::where("visible", 1)->where("on_top", 1)->first();
        if(!empty($top->id))
            $result["top"][] = [
                "id" => $top->id,
                "categoryId" => $top->news_category_id,
                "category" => $top->newsCategory->getTranslation("name", $locale),
                "postDate" => Carbon::parse($top->post_date)->format("Y/m/d"),
                "title" => $top->getTranslation("title", $locale),
                "imgUrl" => $top->news_img_url,
                "imgAlt" => $top->getTranslation("news_img_alt", $locale),
            ];

        //文章列表
        $news = News::where("visible", 1)->where("on_top", 0);
        $news = $news->orderBy("post_date", $request_ordering)->get();
        foreach($news as $item){
            $result["list"][] = [
                "id" => $item->id,
                "categoryId" => $item->news_category_id,
                "category" => $item->newsCategory->getTranslation("name", $locale),
                "postDate" => Carbon::parse($item->post_date)->format("Y/m/d"),
                "title" => $item->getTranslation("title", $locale),
                "imgUrl" => $item->news_img_url,
                "imgAlt" => $item->getTranslation("news_img_alt", $locale),
            ];
        }
        return Response::ok($result);
    }

    public function detail($locale = 'tw', $id){
        $locale = $locale == "tw" ? "zh_TW" : $locale;
        $news = News::find($id);
        $paragraphs = [];
        foreach($news->paragraphs as $paragraph){
            $paragraphs[] = [
                "type" => $paragraph->contentType(),
                "imgUrl" => $paragraph->paragraph_img,
                "alt" => $paragraph->getTranslation("img_alt", $locale),
                "imgOutLink" => $paragraph->image_link,
                "content" => nl2br($paragraph->getTranslation("text_content", $locale)),
            ];
        }

        //瀏覽更多
        $otherNewsList = [];
        $getRangeIds = News::where("visible", 1)->orderByDesc("post_date")->pluck("id")->toArray();
        $helper = new Helper();
        $otherNewsIds = $helper->findArrayTargetIndex($getRangeIds, $id, [-1, 1]);
        $otherNews = News::whereIn('id', $otherNewsIds)->get();
        foreach($otherNews as $item){
            $otherNewsList[] = [
                "id" => $item->id,
                "categoryId" => $item->newsCategory->id,
                "category" => $item->newsCategory->getTranslation("name", $locale),
                "postDate" => Carbon::parse($item->post_date)->format("Y.m.d"),
                "title" => $item->getTranslation("title", $locale),
                "imgUrl" => $item->news_img_url,
                "imgAlt" => $item->getTranslation("news_img_alt", $locale),
                "written" => $item->getTranslation("written_by", $locale),
                "description" => $item->getTranslation("description", $locale),
            ];
        }
        $result = [
            "id" => $news->id,
            "categoryId" => $news->newsCategory->id,
            "category" => $news->newsCategory->getTranslation("name", $locale),
            "postDate" => Carbon::parse($news->post_date)->format("Y.m.d"),
            "title" => $news->getTranslation("title", $locale),
            "imgUrl" => $item->news_img_url,
            "imgAlt" => $item->getTranslation("news_img_alt", $locale),
            "written" => $news->getTranslation("written_by", $locale),
            "description" => $news->getTranslation("description", $locale),
            "metaTitle" => $news->getTranslation("meta_title", $locale),
            "metaKeyword" => $news->getTranslation("meta_keyword", $locale),
            "metaDesc" => $news->getTranslation("meta_description", $locale),
            "metaImg" => $news->meta_img,
            "paragraphs" => $paragraphs,
            "otherNews" => $otherNewsList
        ];
        return Response::ok($result);
    }
}
