<?php

declare(strict_types=1);

namespace App\Http\Controllers\Barangay;

use App\Enums\UserActions;
use App\Http\Controllers\Helpers\Permissions\UserPermissions;
use App\Http\Controllers\Helpers\ResponseHelper;
use App\Models\Barangay\RequestType;
use Hypervel\Http\Request;
use Hypervel\Support\Facades\Auth;
use Hypervel\Support\Facades\Validator;

class RequestTypeController
{
    private function checkWrite(): bool
    {
        return UserPermissions::isActionPermitted(Auth::user()->acct_type, UserActions::ManageRequestTypes);
    }

    public function index()
    {
        $types = RequestType::orderBy('name')->get();
        return response()->json(['success' => true, 'data' => $types]);
    }

    public function active()
    {
        $types = RequestType::active()->orderBy('name')->get();
        return response()->json(['success' => true, 'data' => $types]);
    }

    public function store(Request $request)
    {
        if (!$this->checkWrite()) return ResponseHelper::returnUnauthorized();

        $validator = Validator::make($request->all(), [
            'name'              => 'required|string|max:255',
            'code'              => 'required|string|max:50|unique:barangay_request_types,code',
            'description'       => 'nullable|string',
            'base_fee'          => 'required|numeric|min:0',
            'processing_days'   => 'required|integer|min:1',
            'requires_clearance' => 'nullable|boolean',
        ]);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
        }

        $data = $validator->validated();
        $data['is_active'] = true;
        $data['code'] = strtoupper($data['code']);

        $type = RequestType::create($data);

        return response()->json(['success' => true, 'data' => $type, 'message' => 'Request type created']);
    }

    public function update(Request $request)
    {
        if (!$this->checkWrite()) return ResponseHelper::returnUnauthorized();

        $type = RequestType::where('code', $request->input('target'))->first();
        if (!$type) return ResponseHelper::returnError('Request type not found', 404);

        $validator = Validator::make($request->all(), [
            'name'              => 'nullable|string|max:255',
            'description'       => 'nullable|string',
            'base_fee'          => 'nullable|numeric|min:0',
            'processing_days'   => 'nullable|integer|min:1',
            'requires_clearance' => 'nullable|boolean',
        ]);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
        }

        $type->update($validator->validated());

        return response()->json(['success' => true, 'data' => $type, 'message' => 'Request type updated']);
    }

    public function toggleActive(Request $request)
    {
        if (!$this->checkWrite()) return ResponseHelper::returnUnauthorized();

        $type = RequestType::where('code', $request->input('target'))->first();
        if (!$type) return ResponseHelper::returnError('Request type not found', 404);

        $type->update(['is_active' => !$type->is_active]);

        return response()->json(['success' => true, 'data' => $type]);
    }
}
