<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Admin extends Model
{
    use SoftDeletes;

    protected $table = 'admins';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The data type of the auto-incrementing ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    protected $fillable = [
        'user_id',
        'admin_id',
        'username',
        'details_updated',
        'status',
        'admin_level',
        'role',
    ];

    protected $casts = [
        'admin_id' => 'string',
        'details_updated' => 'boolean',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($admin) {
            if (empty($admin->id)) {
                $admin->id = Str::uuid();
            }
            if (empty($admin->admin_id)) {
                $admin->admin_id = 'ADM_' . strtoupper(Str::random(6));
            }
        });
    }

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

    // Example relationship (if you have blogs authored by admin)
    public function blogs()
    {
        return $this->hasMany(Blog::class, 'author_id', 'admin_id');
    }
}
