<?php

declare(strict_types=1);

namespace ImovelZapAi\Owners\Infrastructure\Persistence\Eloquent;

use Database\Factories\ListingStageFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use ImovelZapAi\Owners\Domain\Enums\ListingStageKey;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;

class ListingStageModel extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuidPrimaryKey;

    protected $table = 'listing_stages';

    protected $fillable = [
        'tenant_id',
        'pipeline_id',
        'key',
        'name',
        'position',
        'is_won',
        'is_lost',
        'color',
    ];

    protected function casts(): array
    {
        return [
            'key' => ListingStageKey::class,
            'position' => 'integer',
            'is_won' => 'boolean',
            'is_lost' => 'boolean',
        ];
    }

    protected static function newFactory(): ListingStageFactory
    {
        return ListingStageFactory::new();
    }

    public function pipeline(): BelongsTo
    {
        return $this->belongsTo(ListingPipelineModel::class, 'pipeline_id');
    }

    public function opportunities(): HasMany
    {
        return $this->hasMany(ListingOpportunityModel::class, 'stage_id');
    }

    public function isTerminal(): bool
    {
        return $this->is_won || $this->is_lost;
    }
}
