<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LegalCase extends Model
{
    protected $table = 'cases';

    protected $fillable = [
        'case_id',
        'case_name',
        'category_code',
        'type_id',
        'priority_id',
        'role_id',
        'date_of_incident',
        'court_appearance_date',
        'notes',
        'case_code',
        'type_name',
        'status_code',
        'cancelled_reason_code',
        'case_reason',
        'created_at',
        'updated_at',
    ];

    public function client()
    {
        return $this->hasOneThrough(
            Client::class,
            CaseClient::class,
            'case_id',
            'id',
            'id',
            'client_id'
        );
    }

    public function clients()
    {
        return $this->belongsToMany(Client::class, 'case_clients', 'case_id', 'client_id')
            ->withPivot(['role_code', 'created_by', 'updated_by'])
            ->withTimestamps();
    }

    public function caseClients()
    {
        return $this->hasMany(CaseClient::class, 'case_id', 'id');
    }

    public function caseCategory()
    {
        return $this->belongsTo(CaseCategory::class, 'category_code', 'category_code');
    }

    public function caseType()
    {
        return $this->belongsTo(CaseType::class, 'type_id', 'type_id');
    }

    public function caseStatus()
    {
        return $this->belongsTo(CaseStatus::class, 'status_code', 'status_code');
    }

    public function cancelledReason()
    {
        return $this->belongsTo(CaseReason::class, 'reason_code', 'reason_code');
    }

    public function caseOutcome()
    {
        return $this->belongsTo(CaseOutcome::class, 'case_outcome', 'id');
    }

    public function parties()
    {
        return $this->hasMany(CaseParty::class, 'case_id', 'id');
    }

    public function caseLawyers()
    {
        return $this->hasMany(CaseLawyer::class, 'case_id', 'id');
    }

    public function matter()
    {
        return $this->belongsTo(Matter::class, 'matter_id', 'id');
    }

    public function lawyer()
    {
        return $this->belongsTo(Lawyer::class, 'lawyer_id', 'lawyer_id');
    }
}
