<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Project;
use App\Models\Region;
use App\Models\Tag;
use App\Supports\Response;
use Carbon\Carbon;
use Illuminate\Http\Request;

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

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

        // 年份清單
        $regions = Region::select(['id', 'name'])->where('visible', 1)->get()->map(function ($record) use ($locale) {
            return [
                'id' => $record->id,
                'name' => $record->getTranslation('name', $locale),
            ];
        });
        $tags = Tag::select(['id', 'name'])->where('visible', 1)->get()->map(function ($record) use ($locale) {
            return [
                'id' => $record->id,
                'name' => $record->getTranslation('name', $locale),
            ];
        });

        $result['regions'] = $regions;
        $result['tags'] = $tags;

        // 文章列表
        $projects = Project::where('visible', true);
        if ($request->has('region')) {
            $projects->where('region_id', $request->input('region'));
        }
        if ($request->has('tags')) {
            $request_tags = $request->input('tags');
            $projects->whereHas('tags', function ($query) use ($request_tags) {
                return $query->whereIn('tags.id', $request_tags);
            });
        }

        $projects = $projects->with(['tags', 'region'])->orderBy('order')->get();
        foreach ($projects as $project) {
            $result['list'][] = [
                'id' => $project->id,
                'region_id' => $project->region_id,
                'region' => $project->region->getTranslation('name', $locale),
                'district' => $project->getTranslation('district', $locale),
                'address' => $project->getTranslation('address', $locale),
                'tags' => $project->tags->map(function ($record) use ($locale) {
                    return [
                        'id' => $record->id,
                        'name' => $record->getTranslation('name', $locale),
                    ];
                }),
                'name' => $project->getTranslation('name', $locale),
                'thumbnail' => $project->thumbnail_url,
                'imgUrl' => $project->first_list_img_url,
                'mobileImgUrl' => $project->first_mobile_img_url,
                'seo_description' => $project->getTranslation('seo_description', $locale),
                'seo_keywords' => $project->getTranslation('seo_keywords', $locale),
            ];
        }

        return Response::ok($result);
    }

    public function detail($locale, $id)
    {

        $locale = $locale == 'tw' ? 'zh_TW' : $locale;
        $project = Project::find($id);

        $projectHistories = [];
        $projectSpaceInfo = [];
        foreach ($project->histories as $history) {
            $operateDate = Carbon::parse($history->operate_date);
            $projectHistories[$operateDate->format('Y')][] = [
                'operateDate' => $operateDate->format('m/d'),
                'title' => $history->getTranslation('title', $locale),
            ];
        }
        foreach ($project->spaceInfos as $spaceInfo) {
            $projectSpaceInfo[] = [
                'title' => $spaceInfo->getTranslation('title', $locale),
                'content' => $spaceInfo->getTranslation('content', $locale),
            ];
        }

        $result = [
            'name' => $project->getTranslation('name', $locale),
            'subName' => $project->getTranslation('sub_name', $locale),
            'description' => $project->getTranslation('summaries', $locale),
            'thumbnail' => $project->thumbnail_url,
            'images' => $project->img_list,
            'mobileImages' => $project->mobile_img_list,
            'region_id' => $project->region_id,
            'region' => $project->region->getTranslation('name', $locale),
            'address' => $project->getTranslation('address', $locale),
            'district' => $project->getTranslation('district', $locale),
            'tags' => $project->tags->map(function ($record) use ($locale) {
                return [
                    'id' => $record->id,
                    'name' => $record->getTranslation('name', $locale),
                ];
            }),
            'floor_plan' => $project->getTranslation('floor_plan', $locale),
            'building_structure' => $project->getTranslation('building_structure', $locale),
            'design_unit' => $project->getTranslation('design_unit', $locale),
            'badge_type' => $project->badge_type,
            'badges' => $project->getBadgesByType($locale, $project->badge_type),
            'contact_info' => [
                'contact' => $project->getTranslation('contact', $locale),
                'officalLink' => $project->offical_link,
            ],
            'spaceInfo' => $projectSpaceInfo,
            'historyList' => $projectHistories,
            'seo_description' => $project->getTranslation('seo_description', $locale),
            'seo_keywords' => $project->getTranslation('seo_keywords', $locale),
        ];

        return Response::ok($result);
    }

    public function badges($locale = 'tw') {
        $locale = $locale == 'tw' ? 'zh_TW' : $locale;

        // 定義「取得時間」的翻譯
        $awardDateLabels = [
            'zh_TW' => '取得時間',
            'en' => 'Awarded',
        ];

        // 取得對應的標籤，如果找不到則使用預設值
        $awardDateLabel = $awardDateLabels[$locale] ?? $awardDateLabels['zh_TW'];

        $projects = Project::where('badge_type', 2)->get();
        $result = [];

        foreach ($projects as $project) {
            $badgesData = $project->awardBadges;
            $badges = [];
            foreach ($badgesData as $badge) {
                $badges[] = [
                    'imgUrl' => $badge->imgUrlLink,
                    'name' => $badge->getTranslation('title', $locale),
                    'rewardYear' => $awardDateLabel . ' : ' . date('Y.m', strtotime($badge->pivot->award_date)),
                ];
            }
            $result[] = [
                'name' => $project->getTranslation('name', $locale),
                'badges' => $badges,
            ];
        }

        return Response::ok($result);
    }
}
