<?php

namespace App\Models;
use Altek\Accountant\Contracts\Recordable;  
use App\Traits\Blameable;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Corporate extends Model
{
    use HasFactory, SoftDeletes, Blameable;

    protected $fillable = [
        'type',
        'parent_id',
        'code',
        'name',
        'payor_id',
        'reason',
        'welcome_message',
        'help_text',
        'active',
        'linking_rules',
        'automatic_linking'
    ];

    protected $casts = [
        'linking_rules' => AsArrayObject::class,
    ];

    protected $appends = [
        'avatar_url',
    ];

    protected $hidden = [
        'created_at',
        'updated_at',
        'deleted_at',
        'created_by',
        'updated_by',
        'deleted_by',
    ];

    public function imports()
    {
        return $this->morphMany(ImportLog::class, 'importable');
    }

    public function files()
    {
        return $this->morphMany(File::class, 'fileable');
    }

    public function avatar()
    {
        return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latest();
    }

    public function getAvatarUrlAttribute()
    {
        return $this->avatar ? $this->avatar->url : null;
    }

    public function plans()
    {
        return $this->hasMany(Plan::class, 'corporate_id', 'id');
    }

    public function policies()
    {
        return $this->hasMany(CorporatePolicy::class);
    }

    public function currentPolicy()
    {
        return $this->hasOne(CorporatePolicy::class)
            ->where('active', true)
            ->latestOfMany();
    }

    public function corporatePlans()
    {
        return $this->hasMany(CorporatePlan::class, 'corporate_id');
    }

    public function corporateBenefits()
    {
        return $this->hasMany(CorporateBenefit::class, 'corporate_id');
    }

    public function corporateDivisions()
    {
        return $this->hasMany(CorporateDivision::class, 'corporate_id');
    }

    public function employees()
    {
        return $this->belongsToMany(Member::class, 'corporate_employees', 'corporate_id', 'member_id')->withPivot([
            'branch_code',
            'division_id',
            'nik',
            'status',
            'start',
            'end'
        ]);
    }

    public function claims()
    {
        return $this->hasManyThrough(Claim::class, CorporateEmployee::class, 'corporate_id', 'member_id', 'id', 'member_id');
    }

    public function importLogs()
    {
        return $this->morphMany(ImportLog::class, 'importable');
    }

    public function limitJournals()
    {
        return $this->morphMany(LimitJournal::class, 'journalable');
    }

    public function services()
    {
        return $this->hasManyThrough(CorporateService::class, Service::class, 'corporate_id', 'service_code', 'id', 'service_code');
    }

    public function corporateServices()
    {
        return $this->hasMany(CorporateService::class, 'corporate_id');
    }

    public function parent()
    {
        return $this->belongsTo(Corporate::class, 'parent_id');
    }

    public function subCorporates()
    {
        return $this->hasMany(Corporate::class, 'parent_id');
    }

    public function getLimitBalanceAttribute()
    {
        return $this->currentPolicy->limit_balance;
    }
}
