<?php

declare(strict_types=1);

namespace App\Listeners\QzTray;

use App\Events\QzTray\CertificateGenerated;
use App\Events\QzTray\CertificateRegenerated;
use App\Events\QzTray\CertificateDeleted;
use App\Events\QzTray\PayloadSigned;
use App\Events\QzTray\SigningFailed;
use App\Repositories\QzTrayLog\QzTrayLogRepositoryInterface;
use Illuminate\Events\Dispatcher;

class LogQzTrayActivity
{
    public function __construct(
        private QzTrayLogRepositoryInterface $logRepository
    ) {}

    /**
     * Handle certificate generated event.
     */
    public function handleCertificateGenerated(CertificateGenerated $event): void
    {
        $this->logRepository->log([
            'user_id' => $event->user?->id,
            'user_name' => $event->user?->name,
            'ip_address' => $event->ipAddress,
            'action' => 'Certificate Generated',
            'result' => 'Success',
            'details' => "Certificate Name: {$event->certificate->name}, Algorithm: {$event->certificate->algorithm}, Fingerprint: {$event->certificate->fingerprint}",
        ]);

        if (function_exists('activity')) {
            activity()
                ->performedOn($event->certificate)
                ->causedBy($event->user)
                ->log("Generated QZ Tray Certificate: {$event->certificate->name}");
        }
    }

    /**
     * Handle certificate regenerated event.
     */
    public function handleCertificateRegenerated(CertificateRegenerated $event): void
    {
        $this->logRepository->log([
            'user_id' => $event->user?->id,
            'user_name' => $event->user?->name,
            'ip_address' => $event->ipAddress,
            'action' => 'Certificate Regenerated',
            'result' => 'Success',
            'details' => "Certificate Name: {$event->certificate->name}, Algorithm: {$event->certificate->algorithm}, Fingerprint: {$event->certificate->fingerprint}",
        ]);

        if (function_exists('activity')) {
            activity()
                ->performedOn($event->certificate)
                ->causedBy($event->user)
                ->log("Regenerated QZ Tray Certificate: {$event->certificate->name}");
        }
    }

    /**
     * Handle certificate deleted event.
     */
    public function handleCertificateDeleted(CertificateDeleted $event): void
    {
        $this->logRepository->log([
            'user_id' => $event->user?->id,
            'user_name' => $event->user?->name,
            'ip_address' => $event->ipAddress,
            'action' => 'Certificate Deleted',
            'result' => 'Success',
            'details' => "Deleted Certificate Name: {$event->certificateName}",
        ]);

        if (function_exists('activity')) {
            activity()
                ->causedBy($event->user)
                ->log("Deleted QZ Tray Certificate: {$event->certificateName}");
        }
    }

    /**
     * Handle payload signed event.
     */
    public function handlePayloadSigned(PayloadSigned $event): void
    {
        $this->logRepository->log([
            'user_id' => $event->user?->id,
            'user_name' => $event->user?->name,
            'ip_address' => $event->ipAddress,
            'action' => 'Signing Request',
            'result' => 'Success',
            'details' => "Successfully signed payload (length: " . strlen($event->payload) . " chars)",
        ]);
    }

    /**
     * Handle signing failed event.
     */
    public function handleSigningFailed(SigningFailed $event): void
    {
        $this->logRepository->log([
            'user_id' => $event->user?->id,
            'user_name' => $event->user?->name,
            'ip_address' => $event->ipAddress,
            'action' => 'Failed Signing',
            'result' => 'Failed',
            'details' => $event->reason,
        ]);
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param Dispatcher $events
     * @return array<string, string>
     */
    public function subscribe(Dispatcher $events): array
    {
        return [
            CertificateGenerated::class => 'handleCertificateGenerated',
            CertificateRegenerated::class => 'handleCertificateRegenerated',
            CertificateDeleted::class => 'handleCertificateDeleted',
            PayloadSigned::class => 'handlePayloadSigned',
            SigningFailed::class => 'handleSigningFailed',
        ];
    }
}
