<?php

namespace App\Models;

use App\Enums\EmployeeAdvanceStatus;
use Illuminate\Database\Eloquent\Attributes\Appends;
use Illuminate\Database\Eloquent\Attributes\Guarded;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

#[Appends(['formatted_amount', 'formatted_created_at', 'formatted_due_date', 'formatted_paid_amount', 'formatted_remaining_amount', 'status_label'])]
#[Guarded(['id'])]
class EmployeeAdvance extends Model
{
    use HasFactory, SoftDeletes;

    protected function casts(): array
    {
        return [
            'status' => EmployeeAdvanceStatus::class,
            'amount' => 'integer',
            'paid_amount' => 'integer',
            'due_date' => 'date:Y-m-d',
            'verified_at' => 'datetime',
            'paid_at' => 'datetime',
        ];
    }

    protected function formattedAmount(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->amount, 0, ',', '.'),
        );
    }

    protected function formattedCreatedAt(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->created_at?->translatedFormat('l, d F Y'),
        );
    }

    protected function formattedDueDate(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->due_date?->translatedFormat('l, d F Y'),
        );
    }

    protected function formattedPaidAmount(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->paid_amount, 0, ',', '.'),
        );
    }

    protected function formattedRemainingAmount(): Attribute
    {
        return Attribute::make(
            get: fn () => 'Rp '.number_format($this->amount - $this->paid_amount, 0, ',', '.'),
        );
    }

    protected function statusLabel(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->status?->label(),
        );
    }

    #[Scope]
    protected function disbursed(Builder $query): void
    {
        $query->where('status', EmployeeAdvanceStatus::DISBURSED);
    }

    #[Scope]
    protected function partial(Builder $query): void
    {
        $query->where('status', EmployeeAdvanceStatus::PARTIAL);
    }

    #[Scope]
    protected function repaid(Builder $query): void
    {
        $query->where('status', EmployeeAdvanceStatus::REPAID);
    }

    #[Scope]
    protected function pending(Builder $query): void
    {
        $query->where('status', EmployeeAdvanceStatus::PENDING);
    }

    #[Scope]
    protected function rejected(Builder $query): void
    {
        $query->where('status', EmployeeAdvanceStatus::REJECTED);
    }

    public function cashTransaction(): BelongsTo
    {
        return $this->belongsTo(CashTransaction::class);
    }

    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

    public function payments(): HasMany
    {
        return $this->hasMany(EmployeeAdvancePayment::class);
    }

    public function paidBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'paid_by_id');
    }

    public function repaymentCashTransaction(): BelongsTo
    {
        return $this->belongsTo(CashTransaction::class, 'repayment_cash_transaction_id');
    }

    public function verifiedBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'verified_by_id');
    }
}
