<?php

namespace App\Http\Controllers;

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

class MypageController extends Controller
{
    public function index()
    {
        $user_id = session('user_id');
        if (!$user_id) {
            return redirect('/login');
        }
        $user = DB::table('user')->where('user_id', $user_id)->first();

        $today = date('Y-m-d');
        // 定期契約情報を取得（park/usertype/psection/ptypeテーブルもJOIN）
        $contracts = DB::table('regular_contract')
            ->join('park', 'regular_contract.park_id', '=', 'park.park_id')
            ->join('usertype', 'regular_contract.user_categoryid', '=', 'usertype.user_categoryid')
            ->leftJoin('city', 'park.city_id', '=', 'city.city_id')
            ->leftJoin('psection', 'regular_contract.psection_id', '=', 'psection.psection_id')
            ->leftJoin('ptype', 'regular_contract.ptype_id', '=', 'ptype.ptype_id')
            ->where('regular_contract.user_id', $user_id)
            ->where('regular_contract.contract_flag', 1)
            ->where('regular_contract.contract_cancel_flag', 0)
            ->where(function ($query) use ($today) {
                $query->where('regular_contract.contract_periode', '>', $today)
                    ->orWhere(function ($q) use ($today) {
                        $q->where('regular_contract.contract_periode', '<=', $today)
                            ->whereRaw('DATEDIFF(?, regular_contract.contract_periode) <= 5', [$today]);
                    });
            })
            ->select(
                'regular_contract.contract_id',
                'park.park_name',
                'usertype.usertype_subject1',
                'regular_contract.contract_periods',
                'regular_contract.contract_periode',
                'regular_contract.enable_months',
                'regular_contract.contract_renewal',
                'regular_contract.park_id',
                'city.update_grace_period_start_date',
                'city.update_grace_period_start_time',
                'city.update_grace_period_end_date',
                'city.update_grace_period_end_time',
                'psection.psection_subject',
                'ptype.ptype_subject',
                'regular_contract.pplace_no'
            )
            ->get();

        // シール情報を取得
        $seals = DB::table('regular_contract')
            ->join('usertype', 'regular_contract.user_categoryid', '=', 'usertype.user_categoryid')
            ->leftJoin('psection', 'regular_contract.psection_id', '=', 'psection.psection_id')
            ->leftJoin('ptype', 'regular_contract.ptype_id', '=', 'ptype.ptype_id')
            ->where('regular_contract.user_id', $user_id)
            ->where('regular_contract.contract_flag', 1)
            ->where('regular_contract.contract_cancel_flag', 0)
            ->where('regular_contract.contract_periode', '>=', $today)
            ->where('regular_contract.contract_permission', 1)
            ->select(
                'regular_contract.contract_id',
                'regular_contract.contract_qr_id',
                'usertype.usertype_subject1',
                'regular_contract.contract_periods',
                'regular_contract.contract_periode',
                'regular_contract.enable_months',
                'regular_contract.contract_renewal',
                'regular_contract.park_id',
                'psection.psection_subject',
                'ptype.ptype_subject',
                'regular_contract.pplace_no'
            )
            ->get();

        // お知らせ情報を取得
        $information = DB::table('user_information_history')
            ->where('user_id', $user_id)
            ->orderBy('user_information_history_id', 'desc')
            ->select('entry_date', 'user_information_history')
            ->first();

        \Log::info('マイページにアクセス', [
            'user_id' => $user_id,
        ]);

        return view('mypage.index', [
            'active_menu' => 'SWO-4-1', // マイページメニューの選択状態用
            'user_name' => $user->user_name, // ユーザー名（ヘッダー用）
            'contracts' => $contracts,
            'seals' => $seals,
            'information' => $information
        ]);
    }
}
