<?php

namespace App\Models\Synapse;

use App\Models\Communication\Call;
use App\Models\Communication\Communication;
use App\Models\Communication\Recording;
use App\Models\Solicitor\Solicitor;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CallScore extends Model
{
    protected $fillable = [
        'solicitor_id',
        'recording_id',
        'communication_id',
        'call_id',
        'is_initial_call',
        'status',
        'total_score',
        'risk_rating',
        'passed',
        'auto_fail',
        'auto_fail_triggers',
        'section_scores',
        'summary',
        'raw_output',
        'error',
    ];

    protected $casts = [
        'is_initial_call'    => 'boolean',
        'passed'             => 'boolean',
        'auto_fail'          => 'boolean',
        'auto_fail_triggers' => 'array',
        'section_scores'     => 'array',
    ];

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

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

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

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

    public function getRiskRatingLabelAttribute(): string
    {
        return match ($this->risk_rating) {
            'low_risk'           => 'Low Risk',
            'acceptable'         => 'Acceptable',
            'compliance_concern' => 'Compliance Concern',
            'high_risk'          => 'High Risk',
            default              => ucfirst((string) $this->risk_rating),
        };
    }

    public function getRiskRatingBadgeAttribute(): string
    {
        return match ($this->risk_rating) {
            'low_risk'           => 'bg-success',
            'acceptable'         => 'bg-primary',
            'compliance_concern' => 'bg-warning text-dark',
            'high_risk'          => 'bg-danger',
            default              => 'bg-secondary',
        };
    }
}
