<?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 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',
        'claim_id',
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

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

    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;
    }
}
