<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Holiday;
use App\Models\Company;
use App\Models\Branch;
use App\Models\Department;
use App\Models\Designation;
use App\Models\Employee;
use Illuminate\Http\Request;

class HolidayController extends Controller
{
    private function getActiveCompanyId()
    {
        return session('active_company_id');
    }

    public function index()
    {
        $companyId = $this->getActiveCompanyId();
        $holidays = Holiday::where('company_id', $companyId)->orderBy('holiday_date')->get();
        return view('admin.holidays.index', compact('holidays'));
    }

    public function create()
    {
        $companyId = $this->getActiveCompanyId();
        $branches     = Branch::where('company_id', $companyId)->get();
        $departments  = Department::where('company_id', $companyId)->get();
        $designations = Designation::where('company_id', $companyId)->get();
        $employees    = Employee::where('company_id', $companyId)->get();
        return view('admin.holidays.create', compact('branches', 'departments', 'designations', 'employees'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'holiday_name' => 'required|string|max:200',
            'holiday_date' => 'required|date',
            'holiday_type' => 'required|in:Full Day,Half Day',
        ]);

        $companyId = $this->getActiveCompanyId();

        Holiday::create([
            'company_id'         => $companyId,
            'superadmin_id'      => \App\Models\Company::find($companyId)->superadmin_id ?? auth()->id(),
            'holiday_name'       => $request->holiday_name,
            'holiday_date'       => $request->holiday_date,
            'holiday_type'       => $request->holiday_type,
            'include_weekly_off' => $request->has('include_weekly_off'),
            'selection_type'     => $request->selection_type ?? 'all_branch', // default to all branch instead of all company
            'branch_ids'         => $request->branch_ids ?? [],
            'department_ids'     => $request->department_ids ?? [],
            'designation_ids'    => $request->designation_ids ?? [],
            'employee_ids'       => $request->employee_ids ?? [],
        ]);

        return redirect()->route('holidays.index')->with('success', 'Holiday created successfully!');
    }

    public function edit($id)
    {
        $companyId = $this->getActiveCompanyId();
        $holiday      = Holiday::where('company_id', $companyId)->findOrFail($id);
        $branches     = Branch::where('company_id', $companyId)->get();
        $departments  = Department::where('company_id', $companyId)->get();
        $designations = Designation::where('company_id', $companyId)->get();
        $employees    = Employee::where('company_id', $companyId)->get();
        return view('admin.holidays.edit', compact('holiday', 'branches', 'departments', 'designations', 'employees'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'holiday_name' => 'required|string|max:200',
            'holiday_date' => 'required|date',
            'holiday_type' => 'required|in:Full Day,Half Day',
        ]);

        $companyId = $this->getActiveCompanyId();
        $holiday = Holiday::where('company_id', $companyId)->findOrFail($id);

        $holiday->update([
            'holiday_name'       => $request->holiday_name,
            'holiday_date'       => $request->holiday_date,
            'holiday_type'       => $request->holiday_type,
            'include_weekly_off' => $request->has('include_weekly_off'),
            'selection_type'     => $request->selection_type ?? 'all_branch', // default to all branch instead of all company
            'branch_ids'         => $request->branch_ids ?? [],
            'department_ids'     => $request->department_ids ?? [],
            'designation_ids'    => $request->designation_ids ?? [],
            'employee_ids'       => $request->employee_ids ?? [],
        ]);

        return redirect()->route('holidays.index')->with('success', 'Holiday updated successfully!');
    }

    public function destroy($id)
    {
        $companyId = $this->getActiveCompanyId();
        Holiday::where('company_id', $companyId)->findOrFail($id)->delete();
        return redirect()->route('holidays.index')->with('success', 'Holiday deleted successfully!');
    }
}
