<?php

namespace App\Models\Dsar;

use App\Models\Claim\CareSetting;
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',
        'care_setting_id',
        'care_setting_is_suggested',
        'page',
        'quote',
        'bbox',
        'category',
        'severity',
        'comment',
        'source',
        'confirmed_at',
        'dismissed_at',
        'human_notes',
    ];

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

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

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

    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) {
            'abuse'        => 'Abuse',
            'neglect'      => 'Neglect',
            'trauma'       => 'Trauma',
            'mental_health'=> 'Mental Health',
            'addiction'    => 'Addiction',
            'violence'     => 'Violence',
            'drug_alcohol' => 'Drug / Alcohol',
            'social_care'  => 'Social Care',
            'suicide'      => 'Suicide',
            // legacy labels — kept so old extracted items still display correctly
            'physical_abuse'        => 'Physical Abuse',
            'emotional_abuse'       => 'Emotional Abuse',
            'financial_abuse'       => 'Financial Abuse',
            '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) {
            'abuse'        => '#dc2626',
            'neglect'      => '#ea580c',
            'trauma'       => '#7c3aed',
            'mental_health'=> '#0891b2',
            'addiction'    => '#d97706',
            'violence'     => '#b91c1c',
            'drug_alcohol' => '#4f46e5',
            'social_care'  => '#0369a1',
            'suicide'      => '#db2777',
            // legacy colours
            'physical_abuse'        => '#dc2626',
            'emotional_abuse'       => '#ea580c',
            'financial_abuse'       => '#ca8a04',
            'self_harm_suicide'     => '#db2777',
            'care_history'          => '#0891b2',
            'medication_error'      => '#16a34a',
            'documentation_failure' => '#4f46e5',
            'safeguarding'          => '#0369a1',
            default                 => '#6b7280',
        };
    }
}
