<?php

namespace App\Http\Controllers;

use App\Exceptions\Approval\AlreadyProcessedException;
use App\Exceptions\Approval\InvalidNewDataException;
use App\Models\Tsisreqdata;
use App\Services\ApprovalService;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ApprovalController extends Controller
{
    public function __construct(private readonly ApprovalService $approvalService)
    {
        $this->middleware('auth');
    }

    // -------------------------------------------------------------------------
    // LIST
    // -------------------------------------------------------------------------

    public function index(Request $request): View
    {
        $status = $request->input('status', '');   // '' = all

        $query = Tsisreqdata::query()
            ->when($status !== '', fn ($q) => $q->where('status', $status))
            ->latest('id');

        $requests = $query->paginate(20)->withQueryString();

        return view('page.approvaldata', compact('requests', 'status'));
    }

    // -------------------------------------------------------------------------
    // DETAIL
    // -------------------------------------------------------------------------

    public function show(int $id): View
    {
        $approvalRequest = Tsisreqdata::findOrFail($id);

    return view('page.detailapprovaldata', compact('approvalRequest'));
    }

    // -------------------------------------------------------------------------
    // APPROVE
    // -------------------------------------------------------------------------

public function approve(int $id): \Illuminate\Http\JsonResponse
{
    try {
        $this->approvalService->approve($id);

        return response()->json([
            'success' => true,
            'message' => "Request #{$id} berhasil disetujui.",
        ]);

    } catch (AlreadyProcessedException $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 422);

    } catch (InvalidNewDataException $e) {
        return response()->json(['success' => false, 'message' => 'Data tidak valid: ' . $e->getMessage()], 422);

    } catch (\Throwable $e) {
        report($e);
        return response()->json(['success' => false, 'message' => 'Terjadi kesalahan, coba lagi.'], 500);
    }
}

public function reject(Request $request, int $id): \Illuminate\Http\JsonResponse
{
    $validated = $request->validate([
        'rejection_reason' => ['required', 'string', 'min:5', 'max:1000'],
    ]);

    try {
        $this->approvalService->reject($id, $validated['rejection_reason']);

        return response()->json([
            'success' => true,
            'message' => "Request #{$id} berhasil ditolak.",
        ]);

    } catch (AlreadyProcessedException $e) {
        return response()->json(['success' => false, 'message' => $e->getMessage()], 422);

    } catch (\Throwable $e) {
        report($e);
        return response()->json(['success' => false, 'message' => 'Terjadi kesalahan, coba lagi.'], 500);
    }
}
}