<?php

namespace App\Domain\Compliance\Models;

use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AccountDeletionRequest extends Model
{
    use HasUuid, BelongsToAcademy, Auditable;

    protected $fillable = [
        'academy_id', 'user_id', 'status', 'reason',
        'requested_at', 'eligible_at', 'completed_at', 'cancelled_at',
        'handled_by', 'admin_notes', 'blockers', 'request_ip',
    ];

    protected function casts(): array
    {
        return [
            'requested_at' => 'datetime',
            'eligible_at' => 'datetime',
            'completed_at' => 'datetime',
            'cancelled_at' => 'datetime',
            'blockers' => 'array',
        ];
    }

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

    public function isOpen(): bool
    {
        return in_array($this->status, ['pending', 'blocked'], true);
    }

    public function statusLabel(): string
    {
        return match ($this->status) {
            'pending' => 'قيد المعالجة',
            'blocked' => 'موقوف — يوجد ما يمنع الحذف',
            'completed' => 'تم الحذف',
            'cancelled' => 'أُلغي الطلب',
            default => $this->status,
        };
    }
}
