<?php

namespace App\Models\Application;

use Carbon\Carbon;
use App\Models\Administration\User;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class StatusType extends Model
{
    use HasFactory;

    protected $table = 'ct_status_types';
    public $timestamps = false;

    protected $fillable = [
        'id',
        'statustypename',
        'visible',
        'available',
        'fk_usercreator',
        'created_at',
        'fk_userupdater',
        'updated_at',
        'is_active'
    ];

    protected $casts = [
        'id' => 'integer',
        'visible' => 'boolean',
        'available' => 'boolean',
        'fk_usercreator' => 'integer',
        'created_at' => 'timestamp',
        'fk_userupdater' => 'integer',
        'updated_at' => 'timestamp',
        'is_active' => 'boolean',
    ];

    protected $hidden = [
        'fk_usercreator',
        'fk_userupdater',
        'is_active',
    ];

    public static function booted()
    {
        static::updated(function ($statustype) {
            Log::channel('statustype')->info('StatusType Updated', [
                'id' => $statustype->id,
                'fk_userupdater' => $statustype->fk_userupdater,
                'updated_at' => Carbon::now(),
                'changes' => $statustype->getChanges()
            ]);
        });
    }

    public function statuses()
    {
        return $this->hasMany(Status::class, 'fk_status_type', 'id')->select('id', 'fk_status_type', 'statusname', 'classname');
    }

    public function user_creator()
    {
        return $this->hasOne(User::class, 'id', 'fk_usercreator')->select('id', 'username');
    }

    public function user_updater()
    {
        return $this->hasOne(User::class, 'id', 'fk_userupdater')->select('id', 'username');
    }
}
