<?php

namespace App\Models\WillsCase;

use App\Interface\DocumentOwner;
use App\Interface\HistoryOwner;
use App\Interface\NoteOwner;
use App\Models\Client\Client;
use App\Trait\HasDocuments;
use App\Trait\HasHistory;
use App\Trait\HasNotes;
use App\Trait\HasSlug;
use App\Trait\HasSmses;
use App\Trait\HasStatus;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class WillsCase extends Model implements DocumentOwner, NoteOwner, HistoryOwner {
    use HasSlug, HasNotes, HasDocuments, HasHistory, HasSmses, HasStatus;

    protected $table = 'wills_cases';

    const REP_OPTIONS = [
        'Andrew Sweeney',
        'John Hunt',
        'Chris Worth',
        'Gavin Hill',
        'Steve Cowans',
    ];

    const CASE_OWNER_OPTIONS = ['SR', 'CA', 'JS'];

    const DRAFTING_CONTACT_OPTIONS = [
        'Will Ainge', 'Zsuzsa', 'Josh', 'Julieanne',
        'Gabrielle', 'Julie', 'Joshua', 'Lottie Ainge',
    ];

    const INTRO_CALL_BY_OPTIONS = ['SR', 'CA', 'JS'];

    const FEE_STATUS_OPTIONS = ['Paid', 'Unpaid'];

    const PRODUCT_OPTIONS = [
        'LPA - Financial',
        'LPA - Health',
        'LPA - Business',
        'Standard Will',
        'Mirror Wills',
        'IPDI Will (Immediate Post Death Interest) (single and mirror)',
        'Letter of Wishes',
        'SEV (severance of tenancy - form A restriction)',
        'Standard Asset Trust',
        'Discretionary Trust',
        'Common Intention Trust',
        'Vulnerable Trust',
        'FAPT (Family Asset Protection Trust)',
        'FAPT+',
        'Protective Will Property Trust',
        'Transfer of Equity',
        'IHT Legacy Trust',
    ];

    const REGISTRATION_DOC_OPTIONS = [
        'LPA - Financial',
        'LPA - Health',
        'LPA - Business',
        'Standard Will',
        'Mirror Wills',
        'IPDI Will (Immediate Post Death Interest) (single and mirror)',
        'Letter of Wishes',
        'SEV (severance of tenancy - form A restriction)',
        'Standard Asset Trust',
        'Discretionary Trust',
        'Common Intention Trust',
        'Vulnerable Trust',
        'FAPT (Family Asset Protection Trust)',
        'FAPT+',
        'Protective Will Property Trust',
        'TR1',
        'RX1',
        'IHT Legacy Trust',
    ];

    protected $fillable = [
        'client_id',
        'status_id',
        'case_number',
        'opened_at',
        'closed_at',
        'is_open',
        'is_closed',
        'ifa_referred_date',
        'national_insurance',
        'ifa_referred',
        'fee_structure',
        'occupation',
        'created_by',
        // New fields
        'rep',
        'case_owner',
        'products',
        'drafting_contact',
        'our_fee',
        'awt_invoice_issued_at',
        'our_fee_status',
        'external_draft_fee',
        'welcome_letter_sent_at',
        'docs_outstanding_from_reps',
        'introduction_call_at',
        'intro_call_by',
        'trust_docs_instructed_at',
        'in_house_instructed_at',
        'drafts_received_at',
        'drafts_issued_to_client_at',
        'master_copies_signed_at',
        'master_copies_received_at',
        'date_on_document_signed',
        'documents_requiring_registration',
        'documents_to_register_aiker',
        'documents_to_register_external',
        'registration_completed_aiker',
        'registration_completed_external',
        'client_letters_from_hmrc_at',
        'annual_minutes_task_set_at',
    ];

    protected $casts = [
        'is_open'                         => 'boolean',
        'is_closed'                       => 'boolean',
        'ifa_referred'                    => 'boolean',
        'documents_requiring_registration' => 'boolean',
        'opened_at'                       => 'datetime',
        'closed_at'                       => 'datetime',
        'ifa_referred_date'               => 'date',
        'awt_invoice_issued_at'           => 'date',
        'welcome_letter_sent_at'          => 'date',
        'introduction_call_at'            => 'date',
        'trust_docs_instructed_at'        => 'date',
        'in_house_instructed_at'          => 'date',
        'drafts_received_at'              => 'date',
        'drafts_issued_to_client_at'      => 'date',
        'master_copies_signed_at'         => 'date',
        'master_copies_received_at'       => 'date',
        'date_on_document_signed'         => 'date',
        'client_letters_from_hmrc_at'     => 'date',
        'annual_minutes_task_set_at'      => 'date',
        'products'                        => 'array',
        'documents_to_register_aiker'     => 'array',
        'documents_to_register_external'  => 'array',
        'registration_completed_aiker'    => 'array',
        'registration_completed_external' => 'array',
    ];

    // -----------------------------------------------------------------------
    // Relationships
    // -----------------------------------------------------------------------

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

    public function comments(): HasMany {
        return $this->hasMany(WillsCaseComment::class)->orderByDesc('comment_created_at');
    }

    // -----------------------------------------------------------------------
    // Query scopes
    // -----------------------------------------------------------------------

    public function scopeOpen($query) {
        return $query->where('is_open', true);
    }

    public function scopeClosed($query) {
        return $query->where('is_closed', true);
    }

    // -----------------------------------------------------------------------
    // Computed attributes
    // -----------------------------------------------------------------------

    protected function displayId(): Attribute {
        return Attribute::make(
            get: fn() => 'WT' . str_pad($this->id, 5, '0', STR_PAD_LEFT),
        );
    }

    // -----------------------------------------------------------------------
    // DocumentOwner contract
    // -----------------------------------------------------------------------

    public function getDocumentStoragePath(): string {
        return 'clients/' . $this->client->slug . '/wills-cases/' . $this->id . '/';
    }
}
