<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\RegistEPaperRequest;
use App\Models\Banner;
use App\Models\EpaperSubscription;
use App\Models\HomepageToTop;
use App\Models\News;
use App\Models\Setting;
use App\Supports\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/**
 * @group Lottery Prize
 */
class HomePageController extends Controller
{
    public function __construct(
    )
    {
    }
    public function banners($locale = 'tw')
    {
        $locale = $locale == "tw" ? "zh_TW" : $locale;
        $result = [];
        $banners = Banner::where("visilbe", true)->get()->map(function ($record) use ($result, $locale){
            $result[] = [
                "imgUrl" => $record->imgUrlLink,
                "mobileImgUrl" => $record->mobileImgLink,
                "title" => $record->getTranslation("title", $locale),
                "content" => $record->getTranslation("content", $locale),
            ];
        });
    }

    public function list($locale = 'tw')
    {
        $locale = $locale == "tw" ? "zh_TW" : $locale;
        $result = [];

        $webNews = News::where("visible", 1)->orderByDesc("post_date")->limit(3)->get();
        foreach($webNews as $item){
            $result["list"][] = [
                "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),
            ];
        }
        return Response::ok($result);
    }
}
