<?php

namespace App\Models;

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

class TreasuryPosition extends Model
{
    use HasFactory;

    protected $fillable = [
        'uuid',
        'position_no',
        'instrument_id',
        'portfolio',
        'customer_id',
        'quantity',
        'cost_amount',
        'market_value',
        'purchase_date',
        'status',
        'created_by',
        'authorized_by',
    ];

    protected static function boot(): void
    {
        parent::boot();

        static::creating(function (self $position): void {
            if (empty($position->uuid)) {
                $position->uuid = (string) Str::uuid();
            }
        });
    }

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

    public function instrument(): BelongsTo
    {
        return $this->belongsTo(TreasuryInstrument::class, 'instrument_id');
    }

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