<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class ParkDetailController extends Controller
{
    public function show($park_id)
    {
        $park = DB::table('park')->where('park_id', $park_id)->first();
        Log::debug('park:', (array)$park);
        $zones = DB::table('zone')
            ->leftJoin('psection', 'zone.psection_id', '=', 'psection.psection_id')
            ->leftJoin('ptype', 'zone.ptype_id', '=', 'ptype.ptype_id')
            ->select('zone.*', 'psection.psection_subject', 'ptype.ptype_subject')
            ->where('zone.park_id', $park_id)
            ->get();
        Log::debug('zones:', $zones->toArray());
        $reserves = DB::table('reserve')
            ->join('zone', function ($join) {
                $join->on('reserve.psection_id', '=', 'zone.psection_id')
                    ->on('reserve.ptype_id', '=', 'zone.ptype_id');
            })
            ->join('ptype', 'zone.ptype_id', '=', 'ptype.ptype_id')
            ->where('reserve.park_id', $park_id)
            ->where('reserve.valid_flag', 1)
            ->select('reserve.*', 'ptype.ptype_subject')
            ->get();
        Log::debug('reserves:', $reserves->toArray());

        $zoneStandardSum = [];
        /*foreach ($zones as $zone) {
            $psectionId = $zone->psection_id;
            if (!isset($zoneStandardSum[$psectionId])) {
                $zoneStandardSum[$psectionId] = 0;
            }
            $zoneStandardSum[$psectionId] += $zone->zone_standard;
        }*/
        $zoneStandardSum = [];
        foreach ($zones as $zone) {
            $key = $zone->psection_subject; // 「自転車」「原付」など
            if (!isset($zoneStandardSum[$key])) {
                $zoneStandardSum[$key] = 0;
            }
            $zoneStandardSum[$key] += $zone->zone_standard;
        }
        Log::debug('zoneStandardSum:', $zoneStandardSum);

        // 空き台数集計用配列
        $vacancyData = [];
        foreach ($zones as $zone) {
            $key = $zone->psection_id . '_' . $zone->ptype_subject;
            if (!isset($vacancyData[$key])) {
                $vacancyData[$key] = 0;
            }
            // zone_tolerance - zone_number を合計
            $vacancyData[$key] += ($zone->zone_tolerance - $zone->zone_number);
        }
        Log::debug('vacancyData:', $vacancyData);

        // reserve件数分を減算
        foreach ($reserves as $reserve) {
            $key = $reserve->psection_id . '_' . $reserve->ptype_subject;
            if (isset($vacancyData[$key])) {
                $vacancyData[$key] -= 1; // 1件分減算
            }
        }

        // 更新期間取得
        $city_grace_periods = DB::table('city')
            ->select('city_id', 'update_grace_period_start_date', 'update_grace_period_start_time', 'update_grace_period_end_date', 'update_grace_period_end_time')
            ->whereIn('city_id', function ($query) {
                $query->select('city_id')->from('park');
            })
            ->get()
            ->keyBy('city_id');
        Log::debug('city_grace_periods:', $city_grace_periods->toArray());

        // 必要なら他テーブルJOINや追加情報も取得
        return response()->json([
            'html' => view('regular_contract.park_detail', compact('park', 'zones', 'reserves', 'zoneStandardSum', 'vacancyData', 'city_grace_periods'))->render()
        ]);
    }
}
