<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Meeting extends Model
{
    use HasFactory;

    protected $fillable = [
        'client_id', 'assigned_to', 'full_name', 'company_name', 'email',
        'phone', 'subject', 'message', 'preferred_date', 'preferred_time',
        'meeting_type', 'status', 'admin_notes', 'meeting_link', 'confirmed_at',
    ];

    protected function casts(): array
    {
        return [
            'preferred_date' => 'date',
            'preferred_time' => 'datetime:H:i',
            'confirmed_at'   => 'datetime',
        ];
    }

    public function client()      { return $this->belongsTo(Client::class); }
    public function assignedUser(){ return $this->belongsTo(User::class, 'assigned_to'); }

    public function scopePending($q)   { return $q->where('status', 'pending'); }
    public function scopeUpcoming($q)  { return $q->where('preferred_date', '>=', now()->toDateString()); }

    public function statusLabel(): string
    {
        return match ($this->status) {
            'pending'     => 'Хүлээгдэж буй',
            'confirmed'   => 'Батлагдсан',
            'completed'   => 'Дууссан',
            'cancelled'   => 'Цуцлагдсан',
            'rescheduled' => 'Дахин товлосон',
            default       => $this->status,
        };
    }

    public function statusColor(): string
    {
        return match ($this->status) {
            'pending'     => 'yellow',
            'confirmed'   => 'green',
            'completed'   => 'blue',
            'cancelled'   => 'red',
            'rescheduled' => 'orange',
            default       => 'gray',
        };
    }

    public function meetingTypeLabel(): string
    {
        return match ($this->meeting_type) {
            'online' => 'Онлайн',
            'office' => 'Оффис дээр',
            'phone'  => 'Утсаар',
            default  => $this->meeting_type,
        };
    }
}
