<?php

namespace App\Models\Datmas;

use App\Models\Admin\Tdaftarexcul;
use App\Models\Admin\Tkelsis;
use App\Models\Admin\Tkelsis1;
use App\Models\Admin\Tstopexcul;
use App\Models\BaseModel;
use App\Models\Saving\PaidBill;
use App\Models\Saving\Tregistrasi;
use App\Models\Saving\Tsignsnk;
use App\Models\Saving\Ttrx;
use App\Models\Spp\Tsalpenrut;
use App\Traits\LogsChanges;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Indentitas extends BaseModel
{
    use HasFactory;
    use LogsChanges;

    protected $connection = 'mai2';

    protected $table = 'tindentitas';

    protected $primaryKey = ['id'];

    protected $appends = ['active'];

    public $incrementing = false;

    protected $fillable = [
        'idmen',
        'idok',
        'tip',
        'nouid',
        'sta',
        'rev',
        'createdby',
        'updatedby',
    ];

    protected $visible = [
        'idok',
        'nouid',
        'spr_tagihan',
        'total_tagihan',
        'tah_tagihan',
        'bulan_tagihan',
        'tagihan',
        'transactions',
        'active',
        'trx',
    ];

    protected $casts = [
        'idmen' => 'integer',
        'idok' => 'integer',
        'tip' => 'integer',
        'sta' => 'integer',
        'rev' => 'integer',
        'createdby' => 'integer',
        'updatedby' => 'integer',
    ];

    public $timestamps = true;

    const CREATED_AT = 'createdat';

    const UPDATED_AT = 'updatedat';

    public function siswa()
    {
        return $this->belongsTo(Siswa::class, 'idok', 'id');
    }

    public function kelsis()
    {
        return $this->belongsTo(Tkelsis::class, 'idok', 'ids');
    }

    public function kelsis1()
    {
        return $this->belongsTo(Tkelsis1::class, 'idok', 'ids');
    }

    public function daftarexcul()
    {
        return $this->belongsTo(Tdaftarexcul::class, 'idok', 'idsis');
    }

    public function stopexcul()
    {
        return $this->belongsTo(Tstopexcul::class, 'idok', 'idsis');
    }

    public function signature()
    {
        return $this->belongsTo(Tsignsnk::class, 'nouid', 'nouid');
    }

    public function registrasi()
    {
        return $this->belongsTo(Tregistrasi::class, 'nouid', 'nouid');
    }

    public function reg()
    {
        return $this->belongsTo(Tregistrasi::class, 'nouid', 'nouid');
    }

    public function trx()
    {
        return $this->hasMany(Ttrx::class, 'nouid', 'nouid');
    }

    public function tagihan()
    {
        return $this->hasMany(Tsalpenrut::class, 'idsis', 'idok');
    }

    public function active(): Attribute
    {
        return Attribute::get(function () {
            return $this->sta !== -1;
        });
    }

    public function getTotalTagihanAttribute()
    {
        return $this->tagihan->sum('jumlah');
    }

    public function paidBill()
    {
        return $this->hasMany(PaidBill::class, 'nouid', 'nouid');
    }

    public function summary(array $except = []): array
    {
        $summary = [
            'active' => (bool) $this->active,
            'pin' => ! is_null($this->siswa->pin ?? null),
            'reg' => $this->reg->sta ?? null,
            'sign' => $this->signature->sign ?? null,
            'version' => $this->signature->snk_version ?? null,
        ];

        if (! empty($except)) {
            $summary = array_diff_key($summary, array_flip($except));
        }

        return $summary;
    }
}
