<?php

declare(strict_types=1);

namespace ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent;

use App\Models\User;
use Database\Factories\VisitFactory;
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\Conversations\Infrastructure\Persistence\Eloquent\ContactModel;
use ImovelZapAi\Conversations\Infrastructure\Persistence\Eloquent\ConversationModel;
use ImovelZapAi\Properties\Infrastructure\Persistence\Eloquent\PropertyInterestModel;
use ImovelZapAi\Properties\Infrastructure\Persistence\Eloquent\PropertyModel;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\BelongsToTenant;
use ImovelZapAi\Shared\Infrastructure\Persistence\Concerns\HasUuidPrimaryKey;
use ImovelZapAi\Visits\Domain\Enums\VisitOutcome;
use ImovelZapAi\Visits\Domain\Enums\VisitPurpose;
use ImovelZapAi\Visits\Domain\Enums\VisitStatus;

/**
 * @property string $id
 * @property string $tenant_id
 * @property VisitStatus|string $status
 * @property VisitOutcome|string|null $outcome
 */
class VisitModel extends Model
{
    use BelongsToTenant;
    use HasFactory;
    use HasUuidPrimaryKey;

    protected $table = 'visits';

    protected $fillable = [
        'tenant_id',
        'contact_id',
        'property_id',
        'conversation_id',
        'property_interest_id',
        'owner_id',
        'listing_opportunity_id',
        'responsible_user_id',
        'created_by',
        'status',
        'purpose',
        'scheduled_start',
        'scheduled_end',
        'timezone',
        'location_notes',
        'confirmed_at',
        'check_in_at',
        'check_out_at',
        'check_in_latitude',
        'check_in_longitude',
        'check_in_notes',
        'outcome',
        'outcome_notes',
        'cancelled_at',
        'cancellation_reason',
        'completed_at',
    ];

    protected function casts(): array
    {
        return [
            'status' => VisitStatus::class,
            'purpose' => VisitPurpose::class,
            'outcome' => VisitOutcome::class,
            'scheduled_start' => 'datetime',
            'scheduled_end' => 'datetime',
            'confirmed_at' => 'datetime',
            'check_in_at' => 'datetime',
            'check_out_at' => 'datetime',
            'cancelled_at' => 'datetime',
            'completed_at' => 'datetime',
        ];
    }

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

    public function contact(): BelongsTo
    {
        return $this->belongsTo(ContactModel::class, 'contact_id');
    }

    public function property(): BelongsTo
    {
        return $this->belongsTo(PropertyModel::class, 'property_id');
    }

    public function conversation(): BelongsTo
    {
        return $this->belongsTo(ConversationModel::class, 'conversation_id');
    }

    public function propertyInterest(): BelongsTo
    {
        return $this->belongsTo(PropertyInterestModel::class, 'property_interest_id');
    }

    public function responsible(): BelongsTo
    {
        return $this->belongsTo(User::class, 'responsible_user_id');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function participants(): HasMany
    {
        return $this->hasMany(VisitParticipantModel::class, 'visit_id');
    }

    public function reminders(): HasMany
    {
        return $this->hasMany(VisitReminderModel::class, 'visit_id');
    }

    public function logs(): HasMany
    {
        return $this->hasMany(VisitLogModel::class, 'visit_id');
    }
}
