<?php

declare(strict_types=1);

namespace App\Domain\Audit\Listeners;

use App\Domain\Account\Events\AccountProvisioned;
use App\Domain\Account\Events\AccountSuspended;
use App\Domain\Account\Events\AccountTerminated;
use App\Domain\Account\Events\AccountUnsuspended;
use App\Domain\Audit\Services\AuditWriter;
use App\Domain\Database\Events\DatabaseCreated;
use App\Domain\Database\Events\DatabaseDeleted;
use App\Domain\Database\Events\DatabaseUserCreated;
use App\Domain\Domain\Events\DomainAdded;
use App\Domain\Domain\Events\DomainRemoved;
use Illuminate\Events\Dispatcher;

/**
 * Tüm domain event'lerini audit_log'a yazan subscriber.
 *
 * Phase 1 Epic 14 — Audit hardening.
 *
 * Bu pattern ile her yeni epic'in event'leri buraya eklenir,
 * her destructive action otomatik logged olur.
 */
final class AuditEventSubscriber
{
    public function handleAccountProvisioned(AccountProvisioned $event): void
    {
        AuditWriter::write(
            action: 'account.provisioned',
            target: $event->account,
            after: ['username' => $event->account->username, 'linux_uid' => $event->account->linux_uid],
        );
    }

    public function handleAccountSuspended(AccountSuspended $event): void
    {
        AuditWriter::write(
            action: 'account.suspended',
            target: $event->account,
            after: ['username' => $event->account->username, 'reason' => $event->reason],
        );
    }

    public function handleAccountUnsuspended(AccountUnsuspended $event): void
    {
        AuditWriter::write(
            action: 'account.unsuspended',
            target: $event->account,
        );
    }

    public function handleAccountTerminated(AccountTerminated $event): void
    {
        AuditWriter::write(
            action: 'account.terminated',
            target: $event->account,
            metadata: ['data_deleted' => $event->dataDeleted],
        );
    }

    public function handleDomainAdded(DomainAdded $event): void
    {
        AuditWriter::write(
            action: 'domain.added',
            target: $event->domain,
            after: ['name' => $event->domain->name, 'type' => $event->domain->type->value],
        );
    }

    public function handleDomainRemoved(DomainRemoved $event): void
    {
        AuditWriter::write(
            action: 'domain.removed',
            metadata: ['account_id' => $event->accountId, 'domain_name' => $event->domainName],
        );
    }

    public function handleDatabaseCreated(DatabaseCreated $event): void
    {
        AuditWriter::write(
            action: 'database.created',
            target: $event->database,
            after: ['real_db_name' => $event->database->real_db_name],
        );
    }

    public function handleDatabaseDeleted(DatabaseDeleted $event): void
    {
        AuditWriter::write(
            action: 'database.deleted',
            metadata: ['account_id' => $event->accountId, 'database_name' => $event->databaseName],
        );
    }

    public function handleDatabaseUserCreated(DatabaseUserCreated $event): void
    {
        AuditWriter::write(
            action: 'database.user.created',
            target: $event->user,
            after: ['real_user_name' => $event->user->real_user_name],
        );
    }

    public function subscribe(Dispatcher $events): array
    {
        return [
            AccountProvisioned::class => 'handleAccountProvisioned',
            AccountSuspended::class => 'handleAccountSuspended',
            AccountUnsuspended::class => 'handleAccountUnsuspended',
            AccountTerminated::class => 'handleAccountTerminated',
            DomainAdded::class => 'handleDomainAdded',
            DomainRemoved::class => 'handleDomainRemoved',
            DatabaseCreated::class => 'handleDatabaseCreated',
            DatabaseDeleted::class => 'handleDatabaseDeleted',
            DatabaseUserCreated::class => 'handleDatabaseUserCreated',
        ];
    }
}
