<?php

namespace Database\Seeders;

use App\Models\EmailTemplate;
use App\Models\ReminderSetting;
use Illuminate\Database\Seeder;

/**
 * Default smart billing reminder templates ({placeholder} syntax) and global settings row.
 */
class ReminderSystemSeeder extends Seeder
{
    public function run(): void
    {
        ReminderSetting::query()->firstOrCreate([], [
            'email_sending_mode' => 'queue',
        ]);

        $wrap = fn (string $inner) => '<div style="font-family:system-ui,Segoe UI,sans-serif;max-width:560px;margin:0 auto;color:#111827;">'
            .'<p style="margin:0 0 16px;">{customer_name}</p>'
            .$inner
            .'<p style="margin:24px 0 0;font-size:13px;color:#6b7280;">{company_name} &middot; {company_email} &middot; {company_phone}<br>{company_address}</p></div>';

        $cta = fn (string $href, string $label) => '<p style="margin:24px 0;"><a href="'.$href.'" style="display:inline-block;padding:12px 20px;background:#4f46e5;color:#fff;text-decoration:none;border-radius:8px;font-weight:600;">'.$label.'</a></p>';

        $templates = [
            [
                'slug' => 'invoice_created',
                'name' => 'Invoice Created',
                'module' => 'invoice',
                'subject' => 'Invoice {invoice_number} from {company_name}',
                'body' => $wrap('<p style="font-size:16px;line-height:1.5;">Your invoice <strong>{invoice_number}</strong> is ready.</p>'
                    .'<p>Total: <strong>{invoice_total}</strong><br>Due: <strong>{invoice_due_date}</strong><br>Balance: <strong>{invoice_balance}</strong></p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'invoice_due_reminder',
                'name' => 'Invoice Due Reminder',
                'module' => 'invoice',
                'subject' => 'Reminder: invoice {invoice_number} due on {invoice_due_date}',
                'body' => $wrap('<p>This is a friendly reminder that invoice <strong>{invoice_number}</strong> will be due on <strong>{invoice_due_date}</strong>.</p>'
                    .'<p>Amount due: <strong>{invoice_balance}</strong></p>'
                    .$cta('{invoice_view_link}', 'View & pay')),
            ],
            [
                'slug' => 'invoice_overdue_reminder',
                'name' => 'Invoice Overdue',
                'module' => 'invoice',
                'subject' => 'Overdue: invoice {invoice_number}',
                'body' => $wrap('<p>Invoice <strong>{invoice_number}</strong> is now overdue. Outstanding balance: <strong>{invoice_balance}</strong>.</p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'invoice_paid',
                'name' => 'Invoice Paid',
                'module' => 'invoice',
                'subject' => 'Payment received — {invoice_number}',
                'body' => $wrap('<p>Thank you. We have recorded payment for invoice <strong>{invoice_number}</strong>.</p>'
                    .'<p>Status: <strong>{invoice_status}</strong></p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'partial_payment_received',
                'name' => 'Partial Payment Received',
                'module' => 'payment',
                'subject' => 'Partial payment received — {invoice_number}',
                'body' => $wrap('<p>We received a partial payment of <strong>{payment_amount}</strong> on {payment_date} (ref: {payment_reference}).</p>'
                    .'<p>Remaining balance: <strong>{invoice_balance}</strong></p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'quotation_created',
                'name' => 'Quotation Created',
                'module' => 'quotation',
                'subject' => 'Quotation {quotation_number} from {company_name}',
                'body' => $wrap('<p>Your quotation <strong>{quotation_number}</strong> is available.</p>'
                    .'<p>Total: <strong>{quotation_total}</strong><br>Valid until: <strong>{quotation_expiry_date}</strong></p>'
                    .$cta('{quotation_view_link}', 'Review quotation')),
            ],
            [
                'slug' => 'quotation_expiry_reminder',
                'name' => 'Quotation Expiry Reminder',
                'module' => 'quotation',
                'subject' => 'Quotation {quotation_number} expires soon',
                'body' => $wrap('<p>Quotation <strong>{quotation_number}</strong> expires on <strong>{quotation_expiry_date}</strong>.</p>'
                    .$cta('{quotation_view_link}', 'Open quotation')),
            ],
            [
                'slug' => 'quotation_accepted',
                'name' => 'Quotation Accepted',
                'module' => 'quotation',
                'subject' => 'Quotation {quotation_number} accepted',
                'body' => $wrap('<p>Thank you — we have recorded your acceptance of quotation <strong>{quotation_number}</strong>.</p>'
                    .$cta('{quotation_view_link}', 'View quotation')),
            ],
            [
                'slug' => 'quotation_rejected',
                'name' => 'Quotation Rejected',
                'module' => 'quotation',
                'subject' => 'Update on quotation {quotation_number}',
                'body' => $wrap('<p>Quotation <strong>{quotation_number}</strong> has been marked as rejected. If you have questions, reply to this email.</p>'),
            ],
            [
                'slug' => 'purchase_order_notification',
                'name' => 'Purchase Order Notification',
                'module' => 'purchase',
                'subject' => 'Purchase order {purchase_order_number} from {company_name}',
                'body' => $wrap('<p>Hello {supplier_name},</p><p>A new purchase order <strong>{purchase_order_number}</strong> has been created. Total: <strong>{purchase_total}</strong>.</p>'),
            ],
            [
                'slug' => 'recurring_invoice_generated',
                'name' => 'Recurring Invoice Generated',
                'module' => 'invoice',
                'subject' => 'New recurring invoice {invoice_number}',
                'body' => $wrap('<p>A new invoice <strong>{invoice_number}</strong> was generated from your recurring schedule.</p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'payment_receipt',
                'name' => 'Payment Receipt',
                'module' => 'payment',
                'subject' => 'Receipt for payment {payment_reference}',
                'body' => $wrap('<p>Payment of <strong>{payment_amount}</strong> received on {payment_date}.</p>'
                    .'<p>Reference: {payment_reference}</p>'
                    .$cta('{invoice_view_link}', 'View invoice')),
            ],
            [
                'slug' => 'manual_reminder',
                'name' => 'Manual Reminder (default subject only)',
                'module' => 'general',
                'subject' => 'Message from {company_name}',
                'body' => $wrap('<p>{customer_name},</p><p>Please review your account at your earliest convenience.</p>'),
            ],
            [
                'slug' => 'follow_up_reminder',
                'name' => 'Follow-up Reminder',
                'module' => 'general',
                'subject' => 'Follow-up from {company_name}',
                'body' => $wrap('<p>{customer_name},</p><p>We are following up regarding your recent activity with us.</p>'),
            ],
            [
                'slug' => 'welcome_email',
                'name' => 'Customer Welcome',
                'module' => 'general',
                'subject' => 'Welcome to {company_name}',
                'body' => $wrap('<p>Hi {customer_name},</p><p>Thank you for choosing <strong>{company_name}</strong>. We are glad to have you with us.</p>'
                    .'<p style="color:#6b7280;font-size:14px;">Date: {today_date}</p>'),
            ],
        ];

        foreach ($templates as $row) {
            EmailTemplate::updateOrCreate(
                ['slug' => $row['slug']],
                [
                    'name' => $row['name'],
                    'subject' => $row['subject'],
                    'body' => $row['body'],
                    'module' => $row['module'],
                    'is_active' => true,
                    'default_template' => true,
                    'variables' => json_encode([]),
                ]
            );
        }
    }
}
