<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\ConversationCommissionStatus;
use App\Enums\ConversationOfferStatus;
use App\Enums\ConversationStatus;
use App\Enums\ConversationVisitStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Conversation extends Model
{
    protected $fillable = [
        'request_id',
        'proposal_id',
        'requester_user_id',
        'proposer_user_id',
        'title',
        'status',
        'visit_status',
        'offer_status',
        'commission_status',
    ];

    protected function casts(): array
    {
        return [
            'status' => ConversationStatus::class,
            'visit_status' => ConversationVisitStatus::class,
            'offer_status' => ConversationOfferStatus::class,
            'commission_status' => ConversationCommissionStatus::class,
        ];
    }

    public function request(): BelongsTo
    {
        return $this->belongsTo(PropertyRequest::class, 'request_id');
    }

    public function proposal(): BelongsTo
    {
        return $this->belongsTo(PropertyProposal::class, 'proposal_id');
    }

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

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

    public function messages(): HasMany
    {
        return $this->hasMany(Message::class);
    }

    public function latestMessage(): HasOne
    {
        return $this->hasOne(Message::class)->latestOfMany();
    }
}
