import type {
  Schema,
  Notification,
  NotificationCategory,
  NotificationChannel,
  NotificationDeliveryAudit,
  NotificationPreference,
  NotificationSubscription,
  NotificationTemplate,
  EventCategoryBinding,
  ChannelRoutingBinding,
} from "../lib/types";

const baseDate = new Date("2024-01-01T00:00:00.000Z");

// NotificationCategory fixtures

export const baseTransactionalCategory = {
  id: "category-1",
  categoryId: "PO_UPDATES",
  displayName: "PO updates",
  optOutAllowed: false,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationCategory<Schema>;

export const baseOptionalCategory = {
  id: "category-2",
  categoryId: "ANNOUNCEMENTS",
  displayName: "Announcements",
  optOutAllowed: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationCategory<Schema>;

// NotificationChannel fixtures

export const baseInAppChannel = {
  id: "channel-1",
  channelId: "IN_APP",
  kind: "PERSONAL",
  displayName: "In-app inbox",
  capabilities: {
    supportsHtmlBody: false,
    supportsAttachments: false,
    supportsRichActions: true,
  },
  enabled: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationChannel<Schema>;

export const baseEmailChannel = {
  id: "channel-2",
  channelId: "EMAIL",
  kind: "PERSONAL",
  displayName: "Email",
  capabilities: {
    supportsHtmlBody: true,
    supportsAttachments: true,
    supportsRichActions: false,
  },
  enabled: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationChannel<Schema>;

export const baseDisabledEmailChannel = {
  ...baseEmailChannel,
  id: "channel-3",
  enabled: false,
} as const satisfies NotificationChannel<Schema>;

export const baseSlackChannel = {
  id: "channel-4",
  channelId: "SLACK",
  kind: "DESTINATION",
  displayName: "Slack",
  capabilities: {
    supportsHtmlBody: false,
    supportsAttachments: true,
    supportsRichActions: true,
  },
  enabled: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationChannel<Schema>;

export const baseDisabledSlackChannel = {
  ...baseSlackChannel,
  id: "channel-5",
  enabled: false,
} as const satisfies NotificationChannel<Schema>;

// ChannelRoutingBinding fixtures

export const baseSlackBinding = {
  id: "binding-slack-1",
  targetType: "PurchaseOrder",
  targetId: "po-1",
  channelId: "SLACK",
  externalChannelRef: "C0123456789",
  displayLabel: "#po-acme",
  isActive: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies ChannelRoutingBinding<Schema>;

export const baseInactiveSlackBinding = {
  ...baseSlackBinding,
  id: "binding-slack-2",
  isActive: false,
} as const satisfies ChannelRoutingBinding<Schema>;

// EventCategoryBinding fixtures

export const baseEventCategoryBinding = {
  id: "binding-1",
  eventType: "PO_ISSUED",
  categoryId: "category-1",
  transactional: true,
  defaultChannels: ["IN_APP", "EMAIL"],
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies EventCategoryBinding<Schema>;

export const baseOptionalEventBinding = {
  id: "binding-2",
  eventType: "ANNOUNCEMENT_PUBLISHED",
  categoryId: "category-2",
  transactional: false,
  defaultChannels: ["IN_APP"],
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies EventCategoryBinding<Schema>;

// NotificationPreference fixtures

export const baseAllowedPreference = {
  id: "preference-1",
  userId: "user-1",
  categoryId: "category-2",
  channelId: "channel-1",
  allowed: true,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationPreference<Schema>;

export const baseOptOutPreference = {
  ...baseAllowedPreference,
  id: "preference-2",
  allowed: false,
} as const satisfies NotificationPreference<Schema>;

// NotificationTemplate fixtures

export const baseTemplate = {
  id: "template-1",
  eventType: "PO_ISSUED",
  channelId: "channel-1",
  locale: "en-US",
  subject: "PO {{poNumber}} issued",
  body: "Purchase order {{poNumber}} has been issued.",
  htmlBody: null,
  variableSchema: '{"variables":[{"name":"poNumber","required":true,"type":"string"}]}',
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationTemplate<Schema>;

export const baseEmailTemplate = {
  ...baseTemplate,
  id: "template-2",
  channelId: "channel-2",
  htmlBody: "<p>Purchase order {{poNumber}} has been issued.</p>",
} as const satisfies NotificationTemplate<Schema>;

export const baseSlackTemplate = {
  ...baseTemplate,
  id: "template-slack",
  channelId: "channel-4",
} as const satisfies NotificationTemplate<Schema>;

// NotificationSubscription fixtures

export const baseSubscription = {
  id: "subscription-1",
  userId: "user-1",
  sourceType: "PURCHASE_ORDER",
  sourceId: "po-1",
  subscribedAt: baseDate,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationSubscription<Schema>;

// Notification fixtures

export const baseQueuedNotification = {
  id: "notification-1",
  recipientUserId: "user-1",
  channelId: "channel-1",
  eventType: "PO_ISSUED",
  sourceType: "PURCHASE_ORDER",
  sourceId: "po-1",
  reason: "SUBSCRIBED",
  locale: "en-US",
  payloadVars: '{"poNumber":"PO-001"}',
  subject: "PO PO-001 issued",
  body: "Purchase order PO-001 has been issued.",
  htmlBody: null,
  idempotencyKey: "PO_ISSUED:po-1:user-1:2024-01-01",
  deliveryStatus: "QUEUED",
  engagementStatuses: [],
  seenAt: null,
  readAt: null,
  archivedAt: null,
  adapterMessageId: null,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies Notification<Schema>;

export const baseSentNotification = {
  ...baseQueuedNotification,
  id: "notification-2",
  deliveryStatus: "SENT",
  adapterMessageId: "sg-msg-1",
} as const satisfies Notification<Schema>;

export const baseDeliveredNotification = {
  ...baseQueuedNotification,
  id: "notification-3",
  deliveryStatus: "DELIVERED",
} as const satisfies Notification<Schema>;

export const baseFailedNotification = {
  ...baseQueuedNotification,
  id: "notification-4",
  deliveryStatus: "FAILED",
} as const satisfies Notification<Schema>;

export const baseSeenNotification = {
  ...baseDeliveredNotification,
  id: "notification-5",
  engagementStatuses: ["SEEN"],
  seenAt: baseDate,
} as const satisfies Notification<Schema>;

export const baseReadNotification = {
  ...baseDeliveredNotification,
  id: "notification-6",
  engagementStatuses: ["SEEN", "READ"],
  seenAt: baseDate,
  readAt: baseDate,
} as const satisfies Notification<Schema>;

export const baseArchivedNotification = {
  ...baseDeliveredNotification,
  id: "notification-7",
  engagementStatuses: ["SEEN", "READ", "ARCHIVED"],
  seenAt: baseDate,
  readAt: baseDate,
  archivedAt: baseDate,
} as const satisfies Notification<Schema>;

// NotificationDeliveryAudit fixtures

export const baseQueuedAudit = {
  id: "audit-1",
  notificationId: "notification-1",
  eventType: "QUEUED",
  occurredAt: baseDate,
  occurredBy: "system",
  errorClass: null,
  errorDetail: null,
  createdAt: baseDate,
  updatedAt: null,
} as const satisfies NotificationDeliveryAudit<Schema>;

export const baseSentAudit = {
  ...baseQueuedAudit,
  id: "audit-2",
  eventType: "SENT",
} as const satisfies NotificationDeliveryAudit<Schema>;

export const baseFailedAudit = {
  ...baseQueuedAudit,
  id: "audit-3",
  eventType: "FAILED",
  errorClass: "AdapterTransportError",
  errorDetail: "SMTP connection refused",
} as const satisfies NotificationDeliveryAudit<Schema>;

export const baseReadAudit = {
  ...baseQueuedAudit,
  id: "audit-4",
  eventType: "READ",
  occurredBy: "user-1",
} as const satisfies NotificationDeliveryAudit<Schema>;
