<?php

namespace Database\Seeders;

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

class EmailTemplateSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $templates = [
            [
                'name' => 'Invoice Created',
                'slug' => 'invoice-created',
                'subject' => 'New Invoice #{{invoice_number}} from {{company_name}}',
                'body' => '<p>Dear {{customer_name}},</p><p>A new invoice has been created for you.</p><p><strong>Invoice Number:</strong> {{invoice_number}}<br><strong>Amount Due:</strong> {{total_amount}}<br><strong>Due Date:</strong> {{due_date}}</p><p>Please find the invoice attached to this email.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['invoice_number', 'customer_name', 'total_amount', 'due_date', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Invoice Paid',
                'slug' => 'invoice-paid',
                'subject' => 'Payment Received - Invoice #{{invoice_number}}',
                'body' => '<p>Dear {{customer_name}},</p><p>We have received your payment for invoice #{{invoice_number}}.</p><p><strong>Amount Paid:</strong> {{amount_paid}}<br><strong>Payment Date:</strong> {{payment_date}}<br><strong>Payment Method:</strong> {{payment_method}}</p><p>Thank you for your business!</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['invoice_number', 'customer_name', 'amount_paid', 'payment_date', 'payment_method', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Invoice Overdue',
                'slug' => 'invoice-overdue',
                'subject' => 'Payment Reminder - Invoice #{{invoice_number}} is Overdue',
                'body' => '<p>Dear {{customer_name}},</p><p>This is a friendly reminder that invoice #{{invoice_number}} is now overdue.</p><p><strong>Invoice Number:</strong> {{invoice_number}}<br><strong>Amount Due:</strong> {{total_amount}}<br><strong>Due Date:</strong> {{due_date}}<br><strong>Days Overdue:</strong> {{days_overdue}}</p><p>Please process the payment at your earliest convenience.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['invoice_number', 'customer_name', 'total_amount', 'due_date', 'days_overdue', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Quotation Sent',
                'slug' => 'quotation-sent',
                'subject' => 'Quotation #{{quote_number}} from {{company_name}}',
                'body' => '<p>Dear {{customer_name}},</p><p>Please find attached our quotation as per your request.</p><p><strong>Quotation Number:</strong> {{quote_number}}<br><strong>Total Amount:</strong> {{total_amount}}<br><strong>Valid Until:</strong> {{expiry_date}}</p><p>Please let us know if you have any questions.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['quote_number', 'customer_name', 'total_amount', 'expiry_date', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Quotation Accepted',
                'slug' => 'quotation-accepted',
                'subject' => 'Quotation #{{quote_number}} Accepted - Thank You!',
                'body' => '<p>Dear {{customer_name}},</p><p>Thank you for accepting our quotation #{{quote_number}}.</p><p>We will now proceed with processing your order. An invoice will be sent to you shortly.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['quote_number', 'customer_name', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Purchase Order Created',
                'slug' => 'purchase-order-created',
                'subject' => 'Purchase Order #{{po_number}} from {{company_name}}',
                'body' => '<p>Dear Supplier,</p><p>Please find attached our purchase order.</p><p><strong>PO Number:</strong> {{po_number}}<br><strong>Total Amount:</strong> {{total_amount}}<br><strong>Delivery Date:</strong> {{delivery_date}}</p><p>Please confirm receipt and expected delivery.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['po_number', 'total_amount', 'delivery_date', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Delivery Challan',
                'slug' => 'delivery-challan',
                'subject' => 'Delivery Challan #{{challan_number}} - Items Shipped',
                'body' => '<p>Dear {{customer_name}},</p><p>Your order has been shipped. Please find the delivery challan attached.</p><p><strong>Challan Number:</strong> {{challan_number}}<br><strong>Invoice Reference:</strong> {{invoice_number}}<br><strong>Expected Delivery:</strong> {{delivery_date}}</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['challan_number', 'customer_name', 'invoice_number', 'delivery_date', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Credit Note Issued',
                'slug' => 'credit-note-issued',
                'subject' => 'Credit Note #{{credit_number}} Issued',
                'body' => '<p>Dear {{customer_name}},</p><p>A credit note has been issued to your account.</p><p><strong>Credit Note Number:</strong> {{credit_number}}<br><strong>Original Invoice:</strong> {{invoice_number}}<br><strong>Credit Amount:</strong> {{credit_amount}}</p><p>This amount will be adjusted in your next invoice.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['credit_number', 'customer_name', 'invoice_number', 'credit_amount', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Welcome Email',
                'slug' => 'welcome-email',
                'subject' => 'Welcome to {{company_name}}!',
                'body' => '<p>Dear {{customer_name}},</p><p>Welcome to {{company_name}}! We are thrilled to have you as our customer.</p><p>Your account has been created successfully. You can now access your customer portal to view invoices, quotations, and manage your account.</p><p>If you have any questions, please don\'t hesitate to contact us.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['customer_name', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Password Reset',
                'slug' => 'password-reset',
                'subject' => 'Reset Your Password',
                'body' => '<p>Dear {{user_name}},</p><p>You have requested to reset your password. Click the link below to create a new password:</p><p><a href="{{reset_link}}">Reset Password</a></p><p>This link will expire in 60 minutes.</p><p>If you did not request this, please ignore this email.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['user_name', 'reset_link', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Account Verification',
                'slug' => 'account-verification',
                'subject' => 'Verify Your Email Address',
                'body' => '<p>Dear {{user_name}},</p><p>Please verify your email address by clicking the link below:</p><p><a href="{{verification_link}}">Verify Email</a></p><p>This link will expire in 24 hours.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['user_name', 'verification_link', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Low Stock Alert',
                'slug' => 'low-stock-alert',
                'subject' => 'Low Stock Alert: {{product_name}}',
                'body' => '<p>Attention,</p><p>The following product is running low on stock:</p><p><strong>Product:</strong> {{product_name}}<br><strong>SKU:</strong> {{product_sku}}<br><strong>Current Stock:</strong> {{current_stock}}<br><strong>Reorder Level:</strong> {{reorder_level}}</p><p>Please arrange for restocking.</p>',
                'variables' => json_encode(['product_name', 'product_sku', 'current_stock', 'reorder_level']),
                'is_active' => true,
            ],
            [
                'name' => 'Out of Stock Alert',
                'slug' => 'out-of-stock-alert',
                'subject' => 'Out of Stock: {{product_name}}',
                'body' => '<p>Attention,</p><p>The following product is now out of stock:</p><p><strong>Product:</strong> {{product_name}}<br><strong>SKU:</strong> {{product_sku}}</p><p>Immediate restocking is required.</p>',
                'variables' => json_encode(['product_name', 'product_sku']),
                'is_active' => true,
            ],
            [
                'name' => 'Order Confirmation',
                'slug' => 'order-confirmation',
                'subject' => 'Order #{{order_number}} Confirmed',
                'body' => '<p>Dear {{customer_name}},</p><p>Thank you for your order! Your order has been confirmed.</p><p><strong>Order Number:</strong> {{order_number}}<br><strong>Order Date:</strong> {{order_date}}<br><strong>Total Amount:</strong> {{total_amount}}</p><p>We will notify you when your order ships.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['order_number', 'customer_name', 'order_date', 'total_amount', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Order Shipped',
                'slug' => 'order-shipped',
                'subject' => 'Order #{{order_number}} Has Been Shipped',
                'body' => '<p>Dear {{customer_name}},</p><p>Great news! Your order has been shipped.</p><p><strong>Order Number:</strong> {{order_number}}<br><strong>Tracking Number:</strong> {{tracking_number}}<br><strong>Carrier:</strong> {{carrier_name}}<br><strong>Estimated Delivery:</strong> {{estimated_delivery}}</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['order_number', 'customer_name', 'tracking_number', 'carrier_name', 'estimated_delivery', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Order Delivered',
                'slug' => 'order-delivered',
                'subject' => 'Order #{{order_number}} Delivered',
                'body' => '<p>Dear {{customer_name}},</p><p>Your order has been delivered!</p><p><strong>Order Number:</strong> {{order_number}}<br><strong>Delivery Date:</strong> {{delivery_date}}</p><p>We hope you enjoy your purchase. Please let us know if you have any feedback.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['order_number', 'customer_name', 'delivery_date', 'company_name']),
                'is_active' => false,
            ],
            [
                'name' => 'Payment Receipt',
                'slug' => 'payment-receipt',
                'subject' => 'Payment Receipt #{{receipt_number}}',
                'body' => '<p>Dear {{customer_name}},</p><p>Thank you for your payment. Please find your receipt below:</p><p><strong>Receipt Number:</strong> {{receipt_number}}<br><strong>Amount:</strong> {{amount}}<br><strong>Payment Date:</strong> {{payment_date}}<br><strong>Payment Method:</strong> {{payment_method}}</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['receipt_number', 'customer_name', 'amount', 'payment_date', 'payment_method', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Refund Processed',
                'slug' => 'refund-processed',
                'subject' => 'Refund Processed - #{{refund_number}}',
                'body' => '<p>Dear {{customer_name}},</p><p>Your refund has been processed.</p><p><strong>Refund Number:</strong> {{refund_number}}<br><strong>Amount:</strong> {{refund_amount}}<br><strong>Original Invoice:</strong> {{invoice_number}}</p><p>The amount will be credited to your original payment method within 5-10 business days.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['refund_number', 'customer_name', 'refund_amount', 'invoice_number', 'company_name']),
                'is_active' => true,
            ],
            [
                'name' => 'Subscription Renewal',
                'slug' => 'subscription-renewal',
                'subject' => 'Subscription Renewal Reminder',
                'body' => '<p>Dear {{customer_name}},</p><p>Your subscription is due for renewal.</p><p><strong>Plan:</strong> {{plan_name}}<br><strong>Renewal Date:</strong> {{renewal_date}}<br><strong>Amount:</strong> {{renewal_amount}}</p><p>No action is needed if you wish to continue - your subscription will renew automatically.</p><p>Best regards,<br>{{company_name}}</p>',
                'variables' => json_encode(['customer_name', 'plan_name', 'renewal_date', 'renewal_amount', 'company_name']),
                'is_active' => false,
            ],
            [
                'name' => 'Monthly Report',
                'slug' => 'monthly-report',
                'subject' => 'Monthly Report - {{month}} {{year}}',
                'body' => '<p>Dear Team,</p><p>Please find attached the monthly report for {{month}} {{year}}.</p><p><strong>Total Revenue:</strong> {{total_revenue}}<br><strong>Total Expenses:</strong> {{total_expenses}}<br><strong>Net Profit:</strong> {{net_profit}}<br><strong>Outstanding Invoices:</strong> {{outstanding_count}}</p><p>Best regards,<br>Finance Team</p>',
                'variables' => json_encode(['month', 'year', 'total_revenue', 'total_expenses', 'net_profit', 'outstanding_count']),
                'is_active' => true,
            ],
        ];

        foreach ($templates as $template) {
            EmailTemplate::updateOrCreate(
                ['slug' => $template['slug']],
                $template
            );
        }
    }
}
