<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
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;

class SupportTicket extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'ticket_reference',
        'customer_email',
        'subject',
        'status',
        'priority',
        'department',
        'department_id',
        'assigned_to',
        'sla_policy_id',
        'first_response_due_at',
        'resolution_due_at',
        'first_responded_at',
        'resolved_at',
        'sla_first_response_breached',
        'sla_resolution_breached',
        'merged_into_ticket_id',
        'last_reply_at',
    ];

    /** @return array<string, string> */
    protected function casts(): array
    {
        return [
            'last_reply_at' => 'datetime',
            'first_response_due_at' => 'datetime',
            'resolution_due_at' => 'datetime',
            'first_responded_at' => 'datetime',
            'resolved_at' => 'datetime',
            'sla_first_response_breached' => 'boolean',
            'sla_resolution_breached' => 'boolean',
        ];
    }

    protected static function booted(): void
    {
        static::created(function (SupportTicket $ticket): void {
            if (empty($ticket->ticket_reference)) {
                $ticket->updateQuietly([
                    'ticket_reference' => sprintf(
                        config('tickets.ticket_reference_format'),
                        $ticket->id
                    ),
                ]);
            }
        });
    }

    public function scopeByReference(Builder $query, string $reference): Builder
    {
        return $query->where('ticket_reference', $reference);
    }

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

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

    public function departmentRelation(): BelongsTo
    {
        return $this->belongsTo(Department::class, 'department_id');
    }

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

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

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany(TicketTag::class, 'support_ticket_tag', 'ticket_id', 'tag_id');
    }

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

    public function satisfactionRating(): HasOne
    {
        return $this->hasOne(TicketSatisfactionRating::class, 'ticket_id');
    }

    public function mergedInto(): BelongsTo
    {
        return $this->belongsTo(self::class, 'merged_into_ticket_id');
    }
}
