<?php

namespace Database\Seeders;

use App\Enums\EmailTemplateCategory;
use App\Models\EmailConfiguration;
use App\Models\EmailTemplate;
use Illuminate\Database\Seeder;

class EmailTemplateSeeder extends Seeder
{
    public function run(): void
    {
        EmailConfiguration::query()->firstOrCreate([], [
            'provider' => 'smtp',
            'encryption' => 'tls',
            'from_name' => config('southend.app_name'),
            'from_address' => config('mail.from.address'),
            'is_enabled' => false,
            'automated_enabled' => true,
        ]);

        $templates = [
            [
                'name' => 'Default Invoice Email',
                'slug' => 'default-invoice',
                'category' => EmailTemplateCategory::Invoice->value,
                'subject' => 'Invoice {{invoice_number}} from {{business_name}}',
                'body_html' => '<p>Dear {{client_name}},</p><p>Please find attached invoice <strong>{{invoice_number}}</strong> dated {{document_date}}.</p><p><strong>Total:</strong> {{currency}} {{total_amount}}<br><strong>Balance Due:</strong> {{currency}} {{balance_due}}<br><strong>Due Date:</strong> {{due_date}}</p><p>{{custom_message}}</p><p>You may view or download your invoice here: <a href="{{download_link}}">{{download_link}}</a></p><p>Thank you,<br>{{sender_name}}</p>',
            ],
            [
                'name' => 'Default Quotation Email',
                'slug' => 'default-quotation',
                'category' => EmailTemplateCategory::Quotation->value,
                'subject' => 'Quotation {{quotation_number}} from {{business_name}}',
                'body_html' => '<p>Dear {{client_name}},</p><p>Please find attached quotation <strong>{{quotation_number}}</strong> valid until {{due_date}}.</p><p><strong>Total:</strong> {{currency}} {{total_amount}}</p><p>{{custom_message}}</p><p>Download: <a href="{{download_link}}">{{download_link}}</a></p>',
            ],
            [
                'name' => 'Default Receipt Email',
                'slug' => 'default-receipt',
                'category' => EmailTemplateCategory::Receipt->value,
                'subject' => 'Payment Receipt {{receipt_number}} — {{business_name}}',
                'body_html' => '<p>Dear {{client_name}},</p><p>Thank you for your payment. Attached is receipt <strong>{{receipt_number}}</strong> for {{currency}} {{amount_paid}} received on {{document_date}}.</p><p>{{custom_message}}</p>',
            ],
            [
                'name' => 'Payment Confirmation',
                'slug' => 'payment-confirmation',
                'category' => EmailTemplateCategory::PaymentConfirmation->value,
                'subject' => 'Payment received — {{payment_reference}}',
                'body_html' => '<p>Dear {{client_name}},</p><p>We have received your payment of {{currency}} {{amount_paid}}. Reference: {{payment_reference}}.</p>',
            ],
            [
                'name' => 'Overdue Invoice Reminder',
                'slug' => 'overdue-reminder',
                'category' => EmailTemplateCategory::OverdueReminder->value,
                'subject' => 'Reminder: Invoice {{invoice_number}} is overdue',
                'body_html' => '<p>Dear {{client_name}},</p><p>This is a friendly reminder that invoice <strong>{{invoice_number}}</strong> with balance {{currency}} {{balance_due}} was due on {{due_date}}.</p><p>Please arrange payment at your earliest convenience.</p>',
            ],
            [
                'name' => 'User Invitation',
                'slug' => 'user-invitation',
                'category' => EmailTemplateCategory::UserInvitation->value,
                'subject' => 'Your {{business_name}} account',
                'body_html' => '<p>Hello {{user_name}},</p><p>An account has been created for you at {{business_name}}.</p><p><strong>Email:</strong> {{client_email}}<br><strong>Temporary password:</strong> {{custom_message}}</p><p>Sign in: <a href="{{document_link}}">{{document_link}}</a></p><p>Please change your password after signing in.</p>',
            ],
            [
                'name' => 'General Message',
                'slug' => 'general-message',
                'category' => EmailTemplateCategory::General->value,
                'subject' => 'Message from {{business_name}}',
                'body_html' => '<p>Dear {{client_name}},</p><p>{{custom_message}}</p><p>Regards,<br>{{sender_name}}</p>',
            ],
            [
                'name' => 'System Notification',
                'slug' => 'system-notification',
                'category' => EmailTemplateCategory::Notification->value,
                'subject' => '{{business_name}} — Notification',
                'body_html' => '<p>Hello {{user_name}},</p><p>{{custom_message}}</p>',
            ],
        ];

        foreach ($templates as $template) {
            EmailTemplate::query()->updateOrCreate(
                ['slug' => $template['slug']],
                array_merge($template, [
                    'is_active' => true,
                    'is_default' => true,
                    'is_system' => true,
                ]),
            );
        }
    }
}
