<?php

namespace App\Jobs\Packages;

use App\Jobs\Concerns\LogsTenantNotifications;
use App\Models\TenantPackage;
use App\Notifications\Packages\TenantPackageExpiredNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;

class SendTenantPackageExpiredNotification implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use LogsTenantNotifications;
    use Queueable;
    use SerializesModels;

    public function __construct(public int $tenantPackageId) {}

    public function handle(): void
    {
        $tenantPackage = TenantPackage::with(['tenant', 'package'])->find($this->tenantPackageId);

        if (! $tenantPackage || ! $tenantPackage->tenant) {
            Log::warning('Tenant package expired job skipped', [
                'tenant_package_id' => $this->tenantPackageId,
            ]);

            return;
        }

        $tenant = $tenantPackage->tenant;

        $context = [
            'tenant_package_id' => $tenantPackage->id,
        ];

        if ($this->isDuplicateNotification($tenant, 'package_expired', $context, ['tenant_package_id'])) {
            $this->logNotification($tenant, [
                'type' => 'package_expired',
                'status' => 'skipped',
                'context' => array_merge($context, ['reason' => 'duplicate']),
            ]);

            return;
        }

        $preferences = app(\App\Services\Packages\TenantNotificationPreferences::class);
        if (! $preferences->shouldNotify($tenant, 'package_expired')) {
            $this->logNotification($tenant, [
                'type' => 'package_expired',
                'status' => 'skipped',
                'context' => array_merge($context, ['reason' => 'opt_out']),
            ]);

            return;
        }

        $emails = collect([
            $tenant->contact_email,
            $tenant->user?->email,
        ])->filter(fn ($email) => is_string($email) && filter_var($email, FILTER_VALIDATE_EMAIL))
            ->unique();

        if ($emails->isEmpty()) {
            Log::info('Tenant package expired notification skipped due to missing recipients', [
                'tenant_package_id' => $tenantPackage->id,
            ]);

            $this->logNotification($tenant, [
                'type' => 'package_expired',
                'status' => 'skipped',
                'context' => array_merge($context, ['reason' => 'no_recipient']),
            ]);

            return;
        }

        $this->dispatchToRecipients(
            $tenant,
            $emails,
            'package_expired',
            function (string $email) use ($tenantPackage) {
                Notification::route('mail', $email)->notify(new TenantPackageExpiredNotification($tenantPackage));
            },
            $context
        );
    }
}
