<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SalaryCertificateRequest;
use App\Models\User;
use App\Services\NotificationService;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
use Inertia\Inertia;
use Inertia\Response as InertiaResponse;

class SalaryCertificateController extends Controller implements HasMiddleware
{
    public static function middleware(): array
    {
        return [
            new Middleware('permission:'.\App\Constants\Permissions::SALARY_CERTIFICATE_MANAGE),
        ];
    }

    public function index(Request $request): InertiaResponse
    {
        $requests = SalaryCertificateRequest::with(['user.employeeProfile.departmentRel'])
            ->orderBy('created_at', 'desc')
            ->paginate(15);

        return Inertia::render('Admin/Payroll/SalaryCertificate/Index', [
            'requests' => $requests,
        ]);
    }

    public function approve(Request $request, SalaryCertificateRequest $salaryCertificateRequest)
    {
        $salaryCertificateRequest->update([
            'status' => SalaryCertificateRequest::STATUS_APPROVED,
            'approved_by' => $request->user()->id,
            'approved_at' => now(),
        ]);

        app(NotificationService::class)->success(
            $salaryCertificateRequest->user_id,
            'Salary Certificate Request Approved',
            'Your salary certificate request has been approved.',
            [
                'request_id' => $salaryCertificateRequest->id,
                'status' => 'approved',
                'type' => 'salary_certificate_status_update',
            ],
            route('employee.profile.edit')
        );

        return back()->with('success', 'Salary certificate request approved successfully.');
    }

    public function reject(Request $request, SalaryCertificateRequest $salaryCertificateRequest)
    {
        $request->validate([
            'rejection_reason' => 'required|string|max:500',
        ]);

        $salaryCertificateRequest->update([
            'status' => SalaryCertificateRequest::STATUS_REJECTED,
            'rejection_reason' => $request->rejection_reason,
            'approved_by' => $request->user()->id,
            'approved_at' => now(),
        ]);

        app(NotificationService::class)->error(
            $salaryCertificateRequest->user_id,
            'Salary Certificate Request Rejected',
            'Your salary certificate request has been rejected.',
            [
                'request_id' => $salaryCertificateRequest->id,
                'status' => 'rejected',
                'type' => 'salary_certificate_status_update',
            ],
            route('employee.profile.edit')
        );

        return back()->with('success', 'Salary certificate request rejected.');
    }

    public function download(User $employee): Response
    {
        $employee->load(['employeeProfile.departmentRel', 'salaryAssignment.payrollStructure']);

        $pdf = Pdf::loadView('pdf.salary_certificate', [
            'employee' => $employee,
            'companyName' => config('branding.theme.app_name', config('app.name')),
        ])->setPaper('a4');

        $filename = 'salary_certificate_'.$employee->name.'.pdf';

        return $pdf->download($filename);
    }
}
