<?php
/*
    * Created by VahabDev in Aparat Live
    * Website: https://pourfi.ir
    * Aparat Live: https://www.aparat.com/vahabdev/live
    * Aparat Channel: https://www.aparat.com/vahabdev
    * Rubika Channel: https://rubika.ir/vahabdev
    * Donate: https://reymit.ir/vahabdev
    * Telegram: https://t.me/vahabdev
*/


namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Ticket extends Model
{
    protected $fillable = [
        'user_id',
        'ticket_number',
        'department',
        'priority',
        'service_id',
        'subject',
        'message',
        'status',
        'date',
        'last_replay',
    ];

    protected $casts = [
        'date' => 'datetime',
        'last_replay' => 'datetime',
    ];

    public function user() : BelongsTo{
        return $this->belongsTo(Client::class, 'user_id', 'id');
    }

    public function TicketReplies() : HasMany{
        return $this->hasMany(TicketReplay::class, 'ticket_id', 'id');
    }

    public function replays(): HasMany
    {
        return $this->hasMany(TicketReplay::class, 'ticket_id');
    }

    public function lastReplay()
    {
        return $this->hasOne(TicketReplay::class)->latestOfMany();
    }

    public function getStatusLabelAttribute(): string
    {
        return match($this->status) {
            'open' => 'باز',
            'user_replay' => 'پاسخ کاربر',
            'admin_replay' => 'پاسخ ادمین',
            'closed' => 'بسته شده',
            default => $this->status,
        };
    }

    public function getPriorityLabelAttribute(): string
    {
        return match($this->priority) {
            'low' => 'کم',
            'medium' => 'متوسط',
            'high' => 'زیاد',
            default => $this->priority,
        };
    }

    public function getDepartmentLabelAttribute(): string
    {
        return match($this->department) {
            'Support' => 'پشتیبانی',
            'Sale' => 'فروش',
            'Management' => 'مدیریت',
            default => $this->department,
        };
    }


}
