<?php

namespace App\Domains\Catalog\Models;

use App\Domains\Cart\Models\Cart;
use App\Domains\Purchase\Models\Purchase;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

#[Fillable([
    'status',
    'expires_at',
    'committed_at',
    'released_at',
    'expired_at',
    'release_reason',
])]
class StockReservation extends Model
{
    public const STATUS_ACTIVE = 'active';

    public const STATUS_COMMITTED = 'committed';

    public const STATUS_RELEASED = 'released';

    public const STATUS_EXPIRED = 'expired';

    protected function casts(): array
    {
        return [
            'expires_at' => 'datetime',
            'committed_at' => 'datetime',
            'released_at' => 'datetime',
            'expired_at' => 'datetime',
        ];
    }

    /** @return HasMany<StockReservationLine, $this> */
    public function lines(): HasMany
    {
        return $this->hasMany(StockReservationLine::class);
    }

    /** @return HasOne<Cart, $this> */
    public function currentCart(): HasOne
    {
        return $this->hasOne(Cart::class, 'current_stock_reservation_id');
    }

    /** @return HasOne<Purchase, $this> */
    public function purchase(): HasOne
    {
        return $this->hasOne(Purchase::class);
    }
}
