<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * One question from a customer, and what became of it.
 */
class SupportRequest extends Model
{
    use HasFactory;
    use HasUuids;

    /** What a customer can pick. Kept short: a long list is a form nobody finishes. */
    public const CATEGORIES = ['technical', 'billing', 'account', 'other'];

    /** Anything not in here is still open as far as the customer is concerned. */
    public const CLOSED = ['answered', 'closed'];

    protected $fillable = [
        'customer_id', 'instance_id', 'subject', 'category', 'body', 'status', 'reported_by', 'answered_at',
    ];

    protected function casts(): array
    {
        return ['answered_at' => 'datetime'];
    }

    public function uniqueIds(): array
    {
        return ['uuid'];
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }

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

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

    public function isOpen(): bool
    {
        return ! in_array($this->status, self::CLOSED, true);
    }
}
