<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Expense extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'date',
        'category',
        'amount',
        'description',
        'receipt_path',
    ];

    protected function casts(): array
    {
        return [
            'date'   => 'date',
            'amount' => 'decimal:2',
        ];
    }

    // ─────────────────────────────────────────
    // CATEGORIES
    // ─────────────────────────────────────────

    public const CATEGORIES = [
        'office_supplies'   => 'Office Supplies',
        'utilities'         => 'Utilities',
        'rent'              => 'Rent',
        'salaries'          => 'Salaries',
        'transport'         => 'Transport',
        'marketing'         => 'Marketing',
        'equipment'         => 'Equipment',
        'software'          => 'Software',
        'professional_fees' => 'Professional Fees',
        'taxes'             => 'Taxes',
        'miscellaneous'     => 'Miscellaneous',
    ];

    // ─────────────────────────────────────────
    // RELATIONSHIPS
    // ─────────────────────────────────────────

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

    // ─────────────────────────────────────────
    // HELPERS
    // ─────────────────────────────────────────

    public function getCategoryLabelAttribute(): string
    {
        return self::CATEGORIES[$this->category] ?? ucfirst($this->category);
    }

    public function getReceiptUrlAttribute(): ?string
    {
        if ($this->receipt_path) {
            return asset('storage/' . $this->receipt_path);
        }
        return null;
    }
}
