<?php

namespace App\Http\Controllers;

use App\Http\Requests\InquiryRequest;
use App\Models\AdmissionApplication;
use App\Models\Inquiry;
use App\Models\SchoolClass;
use App\Support\BranchContext;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
use Illuminate\View\View;

class InquiryController extends Controller implements HasMiddleware
{
    public static function middleware(): array
    {
        return [
            new Middleware('can:viewAny,'.Inquiry::class, only: ['index']),
            new Middleware('can:create,'.Inquiry::class, only: ['create', 'store']),
            new Middleware('can:update,inquiry', only: ['edit', 'update', 'convert']),
            new Middleware('can:delete,inquiry', only: ['destroy']),
        ];
    }

    public function index(): View
    {
        $inquiries = Inquiry::with('schoolClass')->latest()->get();

        return view('inquiries.index', compact('inquiries'));
    }

    public function create(): View
    {
        return view('inquiries.create', ['inquiry' => new Inquiry(), 'classes' => $this->classes()]);
    }

    public function store(InquiryRequest $request, BranchContext $context): RedirectResponse
    {
        $data = $request->validated();
        $branchId = $context->currentId() ?? auth()->user()->branch_id;
        $data['branch_id'] = $branchId;
        $data['inquiry_number'] = Inquiry::nextNumber((int) $branchId);

        Inquiry::create($data);

        return redirect()->route('inquiries.index')->with('success', 'Inquiry recorded.');
    }

    public function edit(Inquiry $inquiry): View
    {
        return view('inquiries.edit', ['inquiry' => $inquiry, 'classes' => $this->classes()]);
    }

    public function update(InquiryRequest $request, Inquiry $inquiry): RedirectResponse
    {
        $inquiry->update($request->validated());

        return redirect()->route('inquiries.index')->with('success', 'Inquiry updated.');
    }

    public function destroy(Inquiry $inquiry): RedirectResponse
    {
        $inquiry->delete();

        return redirect()->route('inquiries.index')->with('success', 'Inquiry deleted.');
    }

    /** Convert an inquiry into an admission application. */
    public function convert(Inquiry $inquiry): RedirectResponse
    {
        $application = AdmissionApplication::create([
            'branch_id' => $inquiry->branch_id,
            'application_number' => AdmissionApplication::nextNumber($inquiry->branch_id),
            'student_name' => $inquiry->student_name,
            'parent_name' => $inquiry->parent_name,
            'contact' => $inquiry->contact,
            'email' => $inquiry->email,
            'class_id' => $inquiry->class_id,
            'inquiry_id' => $inquiry->id,
            'status' => 'pending',
        ]);

        $inquiry->update(['status' => 'converted']);

        return redirect()->route('admissions.show', $application)->with('success', 'Inquiry converted to an admission application.');
    }

    private function classes()
    {
        return SchoolClass::active()->orderBy('level')->get();
    }
}
