<?php

namespace App\Models;

use App\Traits\Multitenantable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Transaction extends Model
{
    use Multitenantable;

    protected $primaryKey = 'uuid';
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'uuid',
        'tenant_id',
        'outlet_id',
        'user_id', 
        'reference_type',
        'reference_id',
        'reference_number',
        'stakeholder',
        'transaction_type',
        'category',
        'amount',
        'notes'
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * Relasi Polimorfik ke Orders atau Purchases
     */
    public function reference(): MorphTo
    {
        return $this->morphTo('reference', 'reference_type', 'reference_id', 'uuid');
    }

    public function tenant(): BelongsTo 
    { 
        return $this->belongsTo(Tenant::class, 'tenant_id', 'uuid'); 
    }
    public function outlet(): BelongsTo 
    { 
        return $this->belongsTo(Outlet::class, 'outlet_id', 'uuid'); 
    }
    public function user(): BelongsTo 
    { 
        return $this->belongsTo(User::class, 'user_id', 'uuid'); 
    }
}
