<?php

namespace App\Http\Controllers\Superadmin;

use App\Http\Controllers\Controller;
use App\Models\DayaTampung;
use App\Models\School;
use Illuminate\Http\Request;

class DayaTampungController extends Controller
{
    /**
     * Display recap of daya tampung from all schools
     */
    public function index(Request $request)
    {
        // Calculate default academic years
        $currentYear = date('Y');
        $currentMonth = date('n');
        
        if ($currentMonth >= 7) {
            $tahunAjaranAktif = $currentYear . '/' . ($currentYear + 1);
            $tahunAjaranBaru = ($currentYear + 1) . '/' . ($currentYear + 2);
        } else {
            $tahunAjaranAktif = ($currentYear - 1) . '/' . $currentYear;
            $tahunAjaranBaru = $currentYear . '/' . ($currentYear + 1);
        }
        
        // Get list of schools with their related daya tampung data
        $schoolsQuery = School::with([
            'dayaTampung' => function($query) use ($tahunAjaranAktif, $tahunAjaranBaru) {
                $query->whereIn('tahun_ajaran', [$tahunAjaranAktif, $tahunAjaranBaru]);
            },
            'kabKota'
        ]);
        
        // Filter by school level
        if ($request->filled('level')) {
            $schoolsQuery->where('level', $request->level);
        }
        
        // Filter by KCD
        if ($request->filled('kcd_id')) {
            $schoolsQuery->where('kcd_id', $request->kcd_id);
        }
        
        // Only get schools that have daya tampung data
        $schoolsQuery->whereHas('dayaTampung');
        
        $schools = $schoolsQuery->orderBy('name', 'asc')->paginate(20);
        
        // Get unique tahun ajaran for filter
        $tahunAjaranList = DayaTampung::select('tahun_ajaran')
            ->distinct()
            ->orderBy('tahun_ajaran', 'desc')
            ->pluck('tahun_ajaran');
        
        return view('superadmin.daya-tampung.index', compact(
            'schools', 
            'tahunAjaranList', 
            'tahunAjaranAktif', 
            'tahunAjaranBaru'
        ));
    }
    
    /**
     * Show detail daya tampung for specific school
     */
    public function show($id)
    {
        // Get the selected daya tampung record (TA Baru)
        $dayaTampungBaru = DayaTampung::with('school.kabKota')->findOrFail($id);
        
        // Calculate active and new academic years
        $currentYear = date('Y');
        $currentMonth = date('n');
        
        if ($currentMonth >= 7) {
            $tahunAjaranAktif = $currentYear . '/' . ($currentYear + 1);
        } else {
            $tahunAjaranAktif = ($currentYear - 1) . '/' . $currentYear;
        }
        
        // Get data for active academic year (Rombel Saat Ini) from the same school
        $dayaTampungAktif = DayaTampung::where('school_id', $dayaTampungBaru->school_id)
            ->where('tahun_ajaran', $tahunAjaranAktif)
            ->first();
        
        return view('superadmin.daya-tampung.show', compact(
            'dayaTampungBaru', 
            'dayaTampungAktif',
            'tahunAjaranAktif'
        ));
    }
}
