<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class NotificationTemplateSeeder extends Seeder
{
    public function run(): void
    {
        $templates = [
            ['trigger_key' => 'registration',          'channel' => 'email', 'subject' => 'Welcome to Job Portal', 'body' => 'Hi {name}, welcome to Job Portal! Your account has been created.'],
            ['trigger_key' => 'registration',          'channel' => 'in_app', 'subject' => null, 'body' => 'Welcome, {name}! Complete your profile to get started.'],

            ['trigger_key' => 'job_application',       'channel' => 'email', 'subject' => 'New application received', 'body' => 'A candidate has applied to your job "{job_title}".'],
            ['trigger_key' => 'job_application',       'channel' => 'in_app', 'subject' => null, 'body' => 'New application for {job_title}.'],
            ['trigger_key' => 'application_status_change', 'channel' => 'email', 'subject' => 'Your application status has changed', 'body' => 'Your application for "{job_title}" is now {status}.'],
            ['trigger_key' => 'application_status_change', 'channel' => 'in_app', 'subject' => null, 'body' => 'Your application for {job_title} is now {status}.'],

            ['trigger_key' => 'interview_invite',      'channel' => 'email', 'subject' => 'Interview scheduled', 'body' => 'You have an interview scheduled for {job_title} on {scheduled_at}.'],
            ['trigger_key' => 'interview_invite',      'channel' => 'in_app', 'subject' => null, 'body' => 'Interview scheduled for {job_title} on {scheduled_at}.'],
            ['trigger_key' => 'interview_reminder',    'channel' => 'email', 'subject' => 'Interview reminder', 'body' => 'Reminder: your interview for {job_title} is at {scheduled_at}.'],
            ['trigger_key' => 'interview_reminder',    'channel' => 'sms',   'subject' => null, 'body' => 'Reminder: interview for {job_title} at {scheduled_at}.'],
            ['trigger_key' => 'job_expired',            'channel' => 'email', 'subject' => 'Your job posting has expired', 'body' => 'Your job posting "{job_title}" has reached its 30-day listing period and is no longer visible to candidates. Repost it or extend it from your Jobs dashboard if you\'d like to keep hiring.'],
            ['trigger_key' => 'job_expired',            'channel' => 'in_app', 'subject' => null, 'body' => 'Your job "{job_title}" has expired after 30 days and is no longer visible to candidates.'],
            ['trigger_key' => 'plan_change_requested',  'channel' => 'in_app', 'subject' => null, 'body' => '{agency_name} requested to switch to the {plan_name} plan. Review it in Platform Reports → Plan Requests.'],
            ['trigger_key' => 'plan_change_approved',   'channel' => 'in_app', 'subject' => null, 'body' => 'Your request to switch to the {plan_name} plan was approved and is now active.'],
            ['trigger_key' => 'plan_change_approved',   'channel' => 'email', 'subject' => 'Your plan change was approved', 'body' => 'Your request to switch to the {plan_name} plan has been approved by our team and is now active on your account.'],
            ['trigger_key' => 'plan_change_rejected',   'channel' => 'in_app', 'subject' => null, 'body' => 'Your request to switch to the {plan_name} plan was declined. {note}'],

            ['trigger_key' => 'payment_success',       'channel' => 'email', 'subject' => 'Payment received', 'body' => 'We have received your payment of {amount} {currency}. Thank you!'],
            ['trigger_key' => 'payment_success',       'channel' => 'in_app', 'subject' => null, 'body' => 'Payment of {amount} {currency} received.'],

            ['trigger_key' => 'subscription_expiry',   'channel' => 'email', 'subject' => 'Your subscription is expiring soon', 'body' => 'Your {plan_name} plan expires on {expiry_date}. Renew to avoid interruption.'],
            ['trigger_key' => 'subscription_expiry',   'channel' => 'in_app', 'subject' => null, 'body' => 'Your subscription expires on {expiry_date}.'],

            ['trigger_key' => 'new_message',           'channel' => 'email', 'subject' => 'You have a new message', 'body' => '{sender_name} sent you a message: "{preview}"'],
            ['trigger_key' => 'new_message',           'channel' => 'in_app', 'subject' => null, 'body' => 'New message from {sender_name}.'],

            ['trigger_key' => 'recruiter_invitation',  'channel' => 'email', 'subject' => 'You have been invited to join an agency', 'body' => '{agency_name} invited you to join as a recruiter. Accept: {invitation_url}'],
        ];

        foreach ($templates as $t) {
            $exists = $this->db->table('notification_templates')
                ->where('trigger_key', $t['trigger_key'])->where('channel', $t['channel'])
                ->get()->getRow();
            if (! $exists) {
                $t['is_active'] = 1;
                $this->db->table('notification_templates')->insert($t);
            }
        }
    }
}
