<?php

namespace App\Http\Controllers;

use App\Http\Resources\LoanResource;
use App\Models\Loan;
use App\Http\Requests\StoreLoanRequest;
use App\Http\Requests\UpdateLoanRequest;
use App\Models\User;
use Illuminate\Support\Facades\Storage;

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

        return LoanResource::collection(Loan::all());
    }

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

        $fields = $request->validated();

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

        $loan = Loan::create($fields);

        // Get all users have the store loans email permission
        $users = User::permission('store loans email')->get();

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

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

        if ($loan) {
            return new LoanResource($loan);
        } else {
            return response()->json(['message' => 'Loan not found'], 404);
        }
    }

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

        // Find loan by ID
        $loan = Loan::find($id);

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

        $fields = $request->validated();

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

        $loan->update($fields);

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

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

        // Find loan by ID
        $loan = Loan::find($id);

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

            return response()->json(['message' => 'Loan deleted successfully'], 200);
        }
    }

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

        // Find loan by ID
        $loan = Loan::find($id);

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

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

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