<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Ticket extends Model
{
    use HasFactory;

    public const STATUS_OPEN_UNASSIGNED = 'open_unassigned';
    public const STATUS_ASSIGNED = 'assigned';
    public const STATUS_IN_PROGRESS = 'in_progress';
    public const STATUS_ANSWERED = 'answered';
    public const STATUS_CLOSED_BY_CUSTOMER = 'closed_by_customer';
    public const STATUS_REOPENED = 'reopened';

    public const CATEGORY_ORDER_ISSUE = 'order_issue';
    public const CATEGORY_PAYMENT = 'payment';
    public const CATEGORY_DELIVERY = 'delivery';
    public const CATEGORY_RETURN_REFUND = 'return_refund';
    public const CATEGORY_OTHER = 'other';

    public const PRIORITY_LOW = 'low';
    public const PRIORITY_MEDIUM = 'medium';
    public const PRIORITY_HIGH = 'high';
    public const PRIORITY_URGENT = 'urgent';

    protected $fillable = [
        'order_id',
        'customer_id',
        'assigned_to',
        'assigned_by',
        'assigned_at',
        'last_replied_by',
        'subject',
        'category',
        'priority',
        'status',
        'active_ticket_key',
        'last_reply_at',
        'customer_reminder_sent_at',
        'closed_at',
        'auto_closed_at',
        'auto_close_reason',
        'closed_by',
    ];

    protected function casts(): array
    {
        return [
            'last_reply_at' => 'datetime',
            'customer_reminder_sent_at' => 'datetime',
            'assigned_at' => 'datetime',
            'closed_at' => 'datetime',
            'auto_closed_at' => 'datetime',
        ];
    }

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function customer()
    {
        return $this->belongsTo(User::class, 'customer_id');
    }

    public function lastReplyBy()
    {
        return $this->belongsTo(User::class, 'last_replied_by');
    }

    public function assignee()
    {
        return $this->belongsTo(User::class, 'assigned_to');
    }

    public function assignedBy()
    {
        return $this->belongsTo(User::class, 'assigned_by');
    }

    public function messages()
    {
        return $this->hasMany(TicketMessage::class);
    }

    public function visibleMessagesForCustomer()
    {
        return $this->messages()->where('is_internal', false);
    }

    public function isClosed(): bool
    {
        return $this->status === self::STATUS_CLOSED_BY_CUSTOMER;
    }

    public function isActive(): bool
    {
        return in_array($this->status, [
            self::STATUS_OPEN_UNASSIGNED,
            self::STATUS_ASSIGNED,
            self::STATUS_IN_PROGRESS,
            self::STATUS_ANSWERED,
            self::STATUS_REOPENED,
        ], true);
    }

    public function statusLabel(): string
    {
        return match ($this->status) {
            self::STATUS_OPEN_UNASSIGNED => 'Open (Unassigned)',
            self::STATUS_ASSIGNED => 'Assigned',
            self::STATUS_IN_PROGRESS => 'In Progress',
            self::STATUS_ANSWERED => 'Answered',
            self::STATUS_CLOSED_BY_CUSTOMER => 'Closed By Customer',
            self::STATUS_REOPENED => 'Reopened',
            default => ucfirst((string) $this->status),
        };
    }
}
