<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class BackupActivityLog extends Model
{
    protected $fillable = [
        'backup_id',
        'action',
        'user_id',
        'user_name',
        'ip_address',
        'details',
    ];

    protected $casts = [
        'details' => 'array',
        'created_at' => 'datetime',
    ];

    /**
     * Relație cu backup-ul
     */
    public function backup(): BelongsTo
    {
        return $this->belongsTo(Backup::class);
    }

    /**
     * Relație cu utilizatorul
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Obține label-ul pentru acțiune
     */
    public function getActionLabelAttribute(): string
    {
        return match($this->action) {
            'created' => 'Creat',
            'downloaded' => 'Descărcat',
            'restored' => 'Restaurat',
            'deleted' => 'Șters',
            'failed' => 'Eșuat',
            default => ucfirst($this->action),
        };
    }

    /**
     * Obține clasa CSS pentru badge-ul acțiunii
     */
    public function getActionBadgeClassAttribute(): string
    {
        return match($this->action) {
            'created' => 'bg-blue-50 dark:bg-blue-900/20 text-blue-700 dark:text-blue-300 border-blue-200 dark:border-blue-800',
            'downloaded' => 'bg-green-50 dark:bg-green-900/20 text-green-700 dark:text-green-300 border-green-200 dark:border-green-800',
            'restored' => 'bg-purple-50 dark:bg-purple-900/20 text-purple-700 dark:text-purple-300 border-purple-200 dark:border-purple-800',
            'deleted' => 'bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-300 border-red-200 dark:border-red-800',
            'failed' => 'bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-300 border-red-200 dark:border-red-800',
            default => 'bg-gray-50 dark:bg-gray-900/50 text-gray-700 dark:text-gray-300 border-gray-200 dark:border-gray-700',
        };
    }

    /**
     * Obține iconița pentru acțiune
     */
    public function getActionIconAttribute(): string
    {
        return match($this->action) {
            'created' => 'fa-plus-circle',
            'downloaded' => 'fa-download',
            'restored' => 'fa-undo',
            'deleted' => 'fa-trash',
            'failed' => 'fa-exclamation-circle',
            default => 'fa-info-circle',
        };
    }

    /**
     * Log o activitate
     */
    public static function logActivity(
        ?int $backupId,
        string $action,
        ?int $userId = null,
        ?string $userName = null,
        ?string $ipAddress = null,
        ?array $details = null
    ): self {
        return self::create([
            'backup_id' => $backupId,
            'action' => $action,
            'user_id' => $userId,
            'user_name' => $userName,
            'ip_address' => $ipAddress,
            'details' => $details,
        ]);
    }
}
