import { z } from 'zod';

// ── Delivery Channels ────────────────────────────────────────

export type DeliveryChannel = 'email' | 'push_apns' | 'push_fcm' | 'sms';
export type DeliveryStatus = 'pending' | 'sent' | 'failed' | 'bounced';

// ── Email Template ───────────────────────────────────────────

export interface EmailTemplate {
  id: string;
  name: string;
  subject: string;
  bodyHtml: string;
  bodyText: string;
  variables: string[];
}

// ── Delivery Request ─────────────────────────────────────────

export const SendEmailSchema = z.object({
  to: z.string().email(),
  templateId: z.string().min(1),
  variables: z.record(z.string()).default({}),
  productId: z.string().min(1),
});

export const SendPushSchema = z.object({
  userId: z.string().min(1),
  title: z.string().min(1),
  body: z.string().min(1),
  data: z.record(z.unknown()).optional(),
  productId: z.string().min(1),
});

export const SendTestEmailSchema = z.object({
  to: z.string().email(),
  templateId: z.string().min(1),
  variables: z.record(z.string()).default({}),
});

export type SendEmailInput = z.infer<typeof SendEmailSchema>;
export type SendPushInput = z.infer<typeof SendPushSchema>;

// ── Delivery Log ─────────────────────────────────────────────

export interface DeliveryLogDoc {
  id: string;
  productId: string;
  pk: string;
  userId?: string;
  channel: DeliveryChannel;
  templateId?: string;
  to: string;
  subject?: string;
  status: DeliveryStatus;
  sentAt?: string;
  error?: string;
  metadata?: Record<string, unknown>;
  createdAt: string;
  _ts?: number;
}

// ── Channel Config ───────────────────────────────────────────

export interface EmailChannelConfig {
  provider: 'sendgrid' | 'postmark' | 'bytelyst' | 'console';
  apiKey?: string;
  fromEmail: string;
  fromName: string;
}

export interface PushChannelConfig {
  apns?: { teamId: string; keyId: string; key: string; bundleId: string };
  fcm?: { projectId: string; serviceAccountKey: string };
}
