<?php

namespace App\Models\Dsar;

use App\Models\Contact\Hub\ContactMessageAttachment;
use App\Models\Document\Document;
use App\Trait\BelongsToSolicitor;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DsarEvidenceItem extends Model
{
    use BelongsToSolicitor;

    protected $fillable = [
        'solicitor_id',
        'contact_message_attachment_id',
        'document_id',
        'dsar_id',
        'page',
        'quote',
        'bbox',
        'category',
        'severity',
        'comment',
        'source',
        'confirmed_at',
        'dismissed_at',
        'human_notes',
    ];

    protected $casts = [
        'bbox'         => 'array',
        'confirmed_at' => 'datetime',
        'dismissed_at' => 'datetime',
    ];

    public function attachment(): BelongsTo
    {
        return $this->belongsTo(ContactMessageAttachment::class, 'contact_message_attachment_id');
    }

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

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

    public function scopeConfirmed(Builder $query): void
    {
        $query->whereNotNull('confirmed_at');
    }

    public function scopeDismissed(Builder $query): void
    {
        $query->whereNotNull('dismissed_at');
    }

    public function scopeUnreviewed(Builder $query): void
    {
        $query->whereNull('confirmed_at')->whereNull('dismissed_at');
    }

    public function scopeBySource(Builder $query, string $source): void
    {
        $query->where('source', $source);
    }

    public function isConfirmed(): bool
    {
        return $this->confirmed_at !== null;
    }

    public function isDismissed(): bool
    {
        return $this->dismissed_at !== null;
    }

    public function isUnreviewed(): bool
    {
        return $this->confirmed_at === null && $this->dismissed_at === null;
    }

    public static function categoryLabel(string $category): string
    {
        return match ($category) {
            'physical_abuse'        => 'Physical Abuse',
            'emotional_abuse'       => 'Emotional Abuse',
            'financial_abuse'       => 'Financial Abuse',
            'neglect'               => 'Neglect',
            'drug_alcohol'          => 'Drug / Alcohol',
            'self_harm_suicide'     => 'Self-Harm / Suicide',
            'care_history'          => 'Care History',
            'medication_error'      => 'Medication Error',
            'documentation_failure' => 'Documentation Failure',
            'safeguarding'          => 'Safeguarding',
            default                 => 'Other',
        };
    }

    public static function categoryColour(string $category): string
    {
        return match ($category) {
            'physical_abuse'        => '#dc2626',
            'emotional_abuse'       => '#ea580c',
            'financial_abuse'       => '#d97706',
            'neglect'               => '#ca8a04',
            'drug_alcohol'          => '#7c3aed',
            'self_harm_suicide'     => '#db2777',
            'care_history'          => '#0891b2',
            'medication_error'      => '#16a34a',
            'documentation_failure' => '#4f46e5',
            'safeguarding'          => '#0369a1',
            default                 => '#6b7280',
        };
    }
}
