<?php

namespace App\Http\Controllers;

use App\Http\Resources\RestitutionResource;
use App\Models\Loan;
use App\Models\Restitution;
use App\Http\Requests\StoreRestitutionRequest;
use App\Http\Requests\UpdateRestitutionRequest;
use Illuminate\Support\Facades\Storage;

class RestitutionController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $this->authorize('view restitutions');

        return RestitutionResource::collection(Restitution::all());
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreRestitutionRequest $request)
    {
        $this->authorize('store restitutions');

        $fields = $request->validated();

        if ($request->hasFile('attachment')) {
            $fields['attachment'] = $request->file('attachment')->store('restitutions', 'public');
        }

        $loan = Loan::find($fields['loan_id']);

        $fields['borrower_id'] = $loan->borrower_id;

        $restitution = Restitution::create($fields);

        return response()->json([
            "restitution" => new RestitutionResource($restitution)
        ], 201);
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        // Find restitution by ID
        $restitution = Restitution::find($id);

        if ($restitution) {
            return new RestitutionResource($restitution);
        } else {
            return response()->json(['message' => 'Restitution not found'], 404);
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateRestitutionRequest $request, $id)
    {
        $this->authorize('update restitutions');

        // Find restitution by ID
        $restitution = Restitution::find($id);

        if (!$restitution) {
            return response()->json(['message' => 'Restitution not found'], 404);
        } elseif ($restitution->is_valid) {
            return response()->json(['message' => 'Cannot update a validated restitution'], 403);
        }

        $fields = $request->validated();

        if ($request->hasFile('attachment')) {
            if ($restitution->attachment) {
                Storage::disk('public')->delete($restitution->attachment);
            }
            $fields['attachment'] = $request->file('attachment')->store('restitutions', 'public');
        }

        $restitution->update($fields);

        return response()->json(["restitution" => new RestitutionResource($restitution)], 200);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $this->authorize('delete restitutions');

        // Find restitution by ID
        $restitution = Restitution::find($id);

        if (!$restitution) {
            return response()->json(['message' => 'Restitution not found'], 404);
        } elseif ($restitution->is_valid) {
            return response()->json(['message' => 'Cannot delete a validated restitution'], 403);
        } else {
            $restitution->delete();
            return response()->json(['message' => 'Restitution deleted successfully'], 200);
        }
    }

    public function validation($id)
    {
        $this->authorize('valid restitutions');

        // Find restitution by ID
        $restitution = Restitution::find($id);

        if (!$restitution) {
            return response()->json(['message' => 'Restitution not found'], 404);
        } elseif ($restitution->is_valid) {
            return response()->json(['error' => 'Restitution is already validated'], 400);
        }

        $restitution->update([
            'is_valid' => true,
            'validation_date' => now(),
        ]);

        return response()->json(["restitution" => new RestitutionResource($restitution)], 200);
    }
}
