<?php

namespace App\Models;

use App\Traits\ApiResponse;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Http\Request;

class Meeting extends Model
{
    protected $fillable = [
        'lead_id',
        'created_by',
        'description',
        'meeting_date',
        'is_interest',
        'interest_reason',
        'is_arrived',
        'arrived_at',
        'is_started',
        'started_at',
        'start_lat',
        'start_long',
        'is_completed',
        'completed_at',
        'end_lat',
        'end_long',
        'is_cancelled',
        'cancelled_at',
        'cancel_reason',
        'is_deleted',
        'created_at',
        'updated_at',
        'otp',
        'otp_expires_at',
        'deleted_at',
    ];

    protected $casts = [
        'lead_id' => 'integer',
        'created_by' => 'integer',
        'meeting_date' => 'datetime',
        'is_interest' => 'integer'
    ];

    public function lead(): BelongsTo
    {
        return $this->belongsTo(Lead::class);
    }

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

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

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

    public function meeting_users(): HasMany
    {
        // Added this because your controller logic uses meeting_users()
        return $this->hasMany(MeetingUser::class);
    }

    public function active_user(): HasOne
    {
        return $this->hasOne(MeetingUser::class)->where('is_active', 1);
    }

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

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

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

}
