<?php

namespace App\Models\Claim;

use App\Interface\DocumentOwner;
use App\Interface\HistoryOwner;
use App\Interface\NoteOwner;
use App\Models\Client\Client;
use App\Models\Communication\Communication;
use App\Models\Dsar\Dsar;
use App\Models\Dsar\DsarTranche;
use App\Models\Institution\Institution;
use App\Models\KeyDate;
use App\Trait\BelongsToSolicitor;
use App\Trait\HasDocuments;
use App\Trait\HasHistory;
use App\Trait\HasNotes;
use App\Trait\HasSlug;
use App\Trait\HasStatus;
use App\Trait\HasTags;
use App\Models\Claim\RedressEstimate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Claim extends Model implements DocumentOwner, NoteOwner, HistoryOwner {
    public $fillable = [
        'solicitor_id',
        'client_id',
        'status_id',
        'slug',
        'redress_type',
        'case_number',
        'redress_case_worker',
        'redress_case_worker_email',
        'claim_id',
        'app_sent_date',
        'app_est_receival_date',
        'app_receival_date',
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
        'app_sent_date' => 'date',
        'app_est_receival_date' => 'date',
        'app_receival_date' => 'date',
    ];

    use BelongsToSolicitor, HasFactory, SoftDeletes, HasSlug, HasNotes, HasDocuments, HasHistory, HasStatus, HasTags;

    protected static function booted(): void {
        // Open the initial status history entry so every claim has a full
        // status timeline from the moment it is created.
        static::created(function (Claim $claim) {
            if (empty($claim->status_id)) {
                return;
            }

            $claim->statusHistory()->create([
                'solicitor_id' => $claim->solicitor_id,
                'status_id'    => $claim->status_id,
                'started_at'   => now(),
                'changed_by'   => auth()->id(),
            ]);
        });
    }

    public function status() {
        return $this->belongsTo(Status::class);
    }

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

    public function careSettings() {
        return $this->hasMany(CareSetting::class);
    }

    public function beneficiary() {
        return $this->hasOne(Beneficiary::class);
    }

    public function furtherInformation() {
        return $this->hasOne(\App\Models\Claim\FurtherInformation::class);
    }

    public function terminalIllness() {
        return $this->hasOne(TerminalIllness::class);
    }

    public function bankDetail() {
        return $this->hasOne(BankDetail::class);
    }

    public function impactDetail() {
        return $this->hasOne(ImpactFactors::class);
    }

    public function communications() {
        return $this->hasMany(Communication::class, 'claim_id');
    }

    public function dsars() {
        return $this->hasMany(Dsar::class);
    }

    public function dsarTranches() {
        return $this->hasMany(DsarTranche::class);
    }

    public function keyDates() {
        return $this->belongsToMany(KeyDate::class, 'claim_key_dates')->withPivot(['date']);
    }

    public function institutions() {
        return $this->belongsToMany(Institution::class, 'claim_institution')
            ->using(ClaimInstitution::class)
            ->withPivot(['id', 'dates'])
            ->withTimestamps();
    }

    public function getDocumentStoragePath(): string {
        return $this->client->getDocumentStoragePath() . "/" . $this->slug;
    }

    public function redressEstimate()
    {
        return $this->hasOne(RedressEstimate::class);
    }
}
