<?php

namespace App\Models;

use App\Enums\CashTransactionType;
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\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

#[Appends(['formatted_amount', 'formatted_balance_after', 'formatted_created_at', 'type_label'])]
#[Guarded(['id'])]
class CashTransaction extends Model implements HasMedia
{
    use HasFactory, InteractsWithMedia, SoftDeletes;

    protected function casts(): array
    {
        return [
            'type' => CashTransactionType::class,
            'amount' => 'integer',
            'balance_after' => 'integer',
        ];
    }

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

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

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

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

    #[Scope]
    protected function deposit(Builder $query): void
    {
        $query->where('type', CashTransactionType::DEPOSIT);
    }

    #[Scope]
    protected function expenseType(Builder $query): void
    {
        $query->where('type', CashTransactionType::EXPENSE);
    }

    #[Scope]
    protected function transfer(Builder $query): void
    {
        $query->where('type', CashTransactionType::TRANSFER);
    }

    #[Scope]
    protected function withdrawal(Builder $query): void
    {
        $query->where('type', CashTransactionType::WITHDRAWAL);
    }

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

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

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

    public function expense(): HasOne
    {
        return $this->hasOne(Expense::class);
    }

    public function order(): HasOne
    {
        return $this->hasOne(Order::class);
    }

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

    public function reference(): MorphTo
    {
        return $this->morphTo();
    }

    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('photos');
    }

    public function registerMediaConversions(?Media $media = null): void
    {
        $this->addMediaConversion('thumb')
            ->fit(Fit::Contain, 150, 150);
    }
}
