<?php

namespace App\Observers;

use App\Models\Credit;
use App\Models\CreditHistory;
use App\Models\TreasuryEntry;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;

class CreditObserver
{
    /**
     * Handle the Credit "created" event.
     */
    public function created(Credit $credit): void
    {
        // Update lender's credit statistics
        $lender = $credit->lender;

        $lender->increment('count_credits');

        $history = new CreditHistory();

        $history->credit_id = $credit->id;
        $history->lender_id = $credit->lender_id;
        $history->user_id = Auth::user()->id;
        $history->code = $credit->code;
        $history->type = $credit->type;
        $history->amount = $credit->amount;
        $history->date = $credit->date;
        $history->due_date = $credit->due_date;
        $history->attachment = $credit->attachment;
        $history->total_repayments = 0;
        $history->transaction = 1;

        $history->save();
    }

    /**
     * Handle the Credit "updated" event.
     */
    public function updated(Credit $credit): void
    {
        $transaction = 2;

        // Check if the credit was just validated
        if ($credit->is_valid && $credit->isDirty('is_valid')) {
            // Update lender's credit statistics
            $lender = $credit->lender;

            $lender->increment('number_credits');
            $lender->increment('total_credits', $credit->amount);
            $lender->in_progress = true;
            $lender->save();

            // Add a new treasury entry
            $treasuryEntry = new TreasuryEntry();

            $treasuryEntry->category_id = 1;
            $treasuryEntry->date = $credit->date;
            $treasuryEntry->amount = $credit->amount;
            $treasuryEntry->title = $credit->code;
            $treasuryEntry->description = "Prêt de la part de '{$lender->name()}'";
            $treasuryEntry->attachment = $credit->attachment;
            $treasuryEntry->is_valid = true;
            $treasuryEntry->validation_date = Carbon::now()->format('Y-m-d H:i:s');

            $treasuryEntry->save();

            $transaction = 3;
        }

        $history = new CreditHistory();

        $history->credit_id = $credit->id;
        $history->lender_id = $credit->lender_id;
        $history->user_id = Auth::user()->id;
        $history->code = $credit->code;
        $history->type = $credit->type;
        $history->amount = $credit->amount;
        $history->date = $credit->date;
        $history->due_date = $credit->due_date;
        $history->attachment = $credit->attachment;
        $history->is_valid = $credit->is_valid;
        $history->validation_date = $credit->validation_date;
        $history->total_repayments = $credit->total_repayments;
        $history->transaction = $transaction;

        $history->save();
    }

    /**
     * Handle the Credit "created" event.
     */
    public function deleted(Credit $credit): void
    {
        // Update lender's credit statistics
        $lender = $credit->lender;

        $lender->decrement('count_credits');

        $history = new CreditHistory();

        $history->credit_id = $credit->id;
        $history->lender_id = $credit->lender_id;
        $history->user_id = Auth::user()->id;
        $history->code = $credit->code;
        $history->type = $credit->type;
        $history->amount = $credit->amount;
        $history->date = $credit->date;
        $history->due_date = $credit->due_date;
        $history->attachment = $credit->attachment;
        $history->is_valid = $credit->is_valid;
        $history->validation_date = $credit->validation_date;
        $history->total_repayments = $credit->total_repayments;
        $history->transaction = 0;

        $history->save();
    }

    /**
     * Handle the Credit "created" event.
     */
    public function restored(Credit $credit): void
    {
        // Update lender's credit statistics
        $lender = $credit->lender;

        $lender->increment('count_credits');
    }
}
