<?php

namespace App\Models\Dsar;

use App\Interface\DocumentOwner;
use App\Interface\HistoryOwner;
use App\Interface\NoteOwner;
use App\Models\Claim\Claim;
use App\Models\Contact\Send\EmailSend;
use App\Models\Institution\Institution;
use App\Models\User\User;
use App\Trait\HasDocuments;
use App\Trait\HasHistory;
use App\Trait\HasNotes;
use App\Trait\HasSlug;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Dsar extends Model implements HistoryOwner, NoteOwner, DocumentOwner {
    public $fillable = [
        'slug',
        'tranche_id',
        'user_id',
        'institution_id',
        'status',
        'is_flagged',
        'claim_id',
        'content',
        'file_path',
        'posted_at',
        'received_at',
        'expected_at',
    ];

    protected $casts = [
        'is_flagged'  => 'boolean',
        'posted_at'   => 'datetime',
        'received_at' => 'datetime',
        'expected_at' => 'datetime',
    ];

    use HasSlug, HasHistory, SoftDeletes, HasNotes, HasDocuments;

    public function user() {
        return $this->belongsTo(User::class);
    }

    public function institution() {
        return $this->belongsTo(Institution::class);
    }

    public function claim() {
        return $this->belongsTo(Claim::class);
    }
    public function tranche() {
        return $this->belongsTo(DsarTranche::class);
    }

    public function responses() {
        return $this->hasMany(DsarResponse::class);
    }

    public function latestResponse() {
        return $this->hasOne(DsarResponse::class)->latestOfMany();
    }

    public function emailSend() {
        return $this->morphOne(EmailSend::class, 'sendable');
    }

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