ALTER TYPE "NotificationType" ADD VALUE IF NOT EXISTS 'ACCOUNT_CREATED';

ALTER TABLE "billing_accounts"
ADD COLUMN "preferredLanguage" TEXT NOT NULL DEFAULT 'en';

ALTER TABLE "notifications"
ADD COLUMN "templateKey" TEXT,
ADD COLUMN "locale" TEXT NOT NULL DEFAULT 'en';

CREATE TABLE "notification_templates" (
  "id" TEXT NOT NULL,
  "templateKey" TEXT NOT NULL,
  "category" TEXT NOT NULL,
  "channel" "NotificationChannel" NOT NULL,
  "locale" TEXT NOT NULL,
  "subject" TEXT,
  "body" TEXT NOT NULL,
  "requiredVariables" JSONB,
  "optionalVariables" JSONB,
  "version" INTEGER NOT NULL DEFAULT 1,
  "isActive" BOOLEAN NOT NULL DEFAULT TRUE,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

  CONSTRAINT "notification_templates_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "notification_templates_templateKey_channel_locale_version_key"
ON "notification_templates"("templateKey", "channel", "locale", "version");

CREATE INDEX "notification_templates_templateKey_channel_locale_isActive_idx"
ON "notification_templates"("templateKey", "channel", "locale", "isActive");

INSERT INTO "notification_templates" (
  "id",
  "templateKey",
  "category",
  "channel",
  "locale",
  "subject",
  "body",
  "requiredVariables",
  "optionalVariables"
)
VALUES
  (
    'notif_tpl_account_created_email_en',
    'account.created',
    'account',
    'EMAIL',
    'en',
    'Your workspace is ready - RentalDriveGo',
    E'Hi {{firstName}},\n\nYour RentalDriveGo workspace for {{companyName}} has been created successfully.\nPlan: {{planName}} ({{billingPeriodLabel}})\nCurrency: {{currency}}\nPrimary payment provider: {{paymentProvider}}\nFree trial ends on {{trialEndDate}}.\n\nYour workspace is ready. Sign in with the email and password you chose during signup.\n\nRentalDriveGo',
    '["firstName","companyName","planName","billingPeriodLabel","currency","paymentProvider","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_account_created_email_fr',
    'account.created',
    'account',
    'EMAIL',
    'fr',
    'Votre espace de travail est pret - RentalDriveGo',
    E'Bonjour {{firstName}},\n\nVotre espace de travail RentalDriveGo pour {{companyName}} a ete cree avec succes.\nForfait : {{planName}} ({{billingPeriodLabel}})\nDevise : {{currency}}\nFournisseur de paiement principal : {{paymentProvider}}\nLa periode d''essai gratuit se termine le {{trialEndDate}}.\n\nVotre espace de travail est pret. Connectez-vous avec l''e-mail et le mot de passe choisis lors de l''inscription.\n\nRentalDriveGo',
    '["firstName","companyName","planName","billingPeriodLabel","currency","paymentProvider","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_account_created_email_ar',
    'account.created',
    'account',
    'EMAIL',
    'ar',
    'مساحة عملك جاهزة - RentalDriveGo',
    E'مرحباً {{firstName}}،\n\nتم إنشاء مساحة عمل RentalDriveGo الخاصة بـ {{companyName}} بنجاح.\nالخطة: {{planName}} ({{billingPeriodLabel}})\nالعملة: {{currency}}\nمزود الدفع الرئيسي: {{paymentProvider}}\nتنتهي الفترة التجريبية المجانية في {{trialEndDate}}.\n\nمساحة عملك جاهزة. سجّل الدخول باستخدام البريد الإلكتروني وكلمة المرور التي اخترتهما عند التسجيل.\n\nRentalDriveGo',
    '["firstName","companyName","planName","billingPeriodLabel","currency","paymentProvider","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_account_created_in_app_en',
    'account.created',
    'account',
    'IN_APP',
    'en',
    'Workspace ready',
    E'Your RentalDriveGo workspace for {{companyName}} is ready. Your free trial ends on {{trialEndDate}}.',
    '["companyName","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_account_created_in_app_fr',
    'account.created',
    'account',
    'IN_APP',
    'fr',
    'Espace pret',
    E'Votre espace de travail RentalDriveGo pour {{companyName}} est pret. Votre essai gratuit se termine le {{trialEndDate}}.',
    '["companyName","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_account_created_in_app_ar',
    'account.created',
    'account',
    'IN_APP',
    'ar',
    'مساحة العمل جاهزة',
    E'مساحة عمل RentalDriveGo الخاصة بـ {{companyName}} جاهزة. تنتهي الفترة التجريبية المجانية في {{trialEndDate}}.',
    '["companyName","trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_email_en',
    'booking.confirmed',
    'reservation',
    'EMAIL',
    'en',
    'Booking confirmed - {{companyName}}',
    E'Hi {{firstName}},\n\nYour booking with {{companyName}} has been confirmed.\nPickup: {{startDate}}\nReturn: {{endDate}}\nVehicle: {{vehicleName}}\n\nThank you for choosing RentalDriveGo.',
    '["firstName","companyName","startDate","endDate","vehicleName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_email_fr',
    'booking.confirmed',
    'reservation',
    'EMAIL',
    'fr',
    'Reservation confirmee - {{companyName}}',
    E'Bonjour {{firstName}},\n\nVotre reservation chez {{companyName}} a ete confirmee.\nPrise en charge : {{startDate}}\nRetour : {{endDate}}\nVehicule : {{vehicleName}}\n\nMerci d''avoir choisi RentalDriveGo.',
    '["firstName","companyName","startDate","endDate","vehicleName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_email_ar',
    'booking.confirmed',
    'reservation',
    'EMAIL',
    'ar',
    'تم تأكيد الحجز - {{companyName}}',
    E'مرحباً {{firstName}}،\n\nتم تأكيد حجزك مع {{companyName}}.\nالاستلام: {{startDate}}\nالإرجاع: {{endDate}}\nالمركبة: {{vehicleName}}\n\nشكراً لاختيار RentalDriveGo.',
    '["firstName","companyName","startDate","endDate","vehicleName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_in_app_en',
    'booking.confirmed',
    'reservation',
    'IN_APP',
    'en',
    'Booking confirmed',
    E'Your booking with {{companyName}} has been confirmed.',
    '["companyName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_in_app_fr',
    'booking.confirmed',
    'reservation',
    'IN_APP',
    'fr',
    'Reservation confirmee',
    E'Votre reservation chez {{companyName}} a ete confirmee.',
    '["companyName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_booking_confirmed_in_app_ar',
    'booking.confirmed',
    'reservation',
    'IN_APP',
    'ar',
    'تم تأكيد الحجز',
    E'تم تأكيد حجزك مع {{companyName}}.',
    '["companyName"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_subscription_trial_ending_in_app_en',
    'subscription.trial_ending',
    'subscription',
    'IN_APP',
    'en',
    'Your trial ends soon',
    E'Your free trial ends on {{trialEndDate}}. Add a payment method to keep access.',
    '["trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_subscription_trial_ending_in_app_fr',
    'subscription.trial_ending',
    'subscription',
    'IN_APP',
    'fr',
    'Votre essai se termine bientot',
    E'Votre essai gratuit se termine le {{trialEndDate}}. Ajoutez un moyen de paiement pour conserver l''acces.',
    '["trialEndDate"]'::jsonb,
    '[]'::jsonb
  ),
  (
    'notif_tpl_subscription_trial_ending_in_app_ar',
    'subscription.trial_ending',
    'subscription',
    'IN_APP',
    'ar',
    'ستنتهي الفترة التجريبية قريباً',
    E'تنتهي فترتك التجريبية المجانية في {{trialEndDate}}. أضف وسيلة دفع للحفاظ على الوصول.',
    '["trialEndDate"]'::jsonb,
    '[]'::jsonb
  );
