<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\News;
use App\Models\NewsCategory;
use App\Models\Project;
use App\Models\Region;
use App\Models\Tag;
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 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'])->orderByDesc("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),
                "imgUrl" => $project->first_list_img_url
            ];
        }
        return Response::ok($result);
    }

    public function detail($locale = 'tw', $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),
            "images" => $project->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" => [
                "unitName" => $project->getTranslation("contact_unit", $locale),
                "unitPhone" => $project->getTranslation("contact_phone", $locale),
                "inversmentPhone" => $project->getTranslation("inversment_phone", $locale),
                "officalLink" => $project->offical_link,
            ],
            "spaceInfo" => $projectSpaceInfo,
            "historyList" => $projectHistories
        ];
        return Response::ok($result);
    }

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

        $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" => "取得時間 : " . date("Y.m", strtotime($badge->pivot->award_date))
                ];
            }
            $result[] = [
                "name" => $project->getTranslation("name", $locale),
                "badges" => $badges
            ];
        }

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