// Types

import { CursorPaginatedItem } from "hooks/usePagination";
import { TemplateDto } from "./templates";
import {
  CampaignScheduleType,
  CampaignStatus as SMSCampaignStatus,
  ScheduleType,
} from "@komi-app/messaging-sdk";
import { FormValues, SmartScheduleFormTabs } from "@komi-app/creator-ui";

/** @deprecated Use @komi-app/messaging-sdk */
export enum CampaignStatus {
  Draft = "Draft",
  Registered = "Registered",
  Scheduled = "Scheduled",
  Sending = "Sending",
  Completed = "Completed",
  Stopped = "Stopped",
}

/** @deprecated */
export interface Campaign extends CampaignDto {
  campaignId: string;
  talentProfileId?: string;
  createdAt: number;
  updatedAt: number;
}
export type CampaignWithCursor = CursorPaginatedItem<Campaign>;

/** @deprecated */
export interface CampaignDto {
  name: string;
  subject: string;
  status: CampaignStatus;
  schedule: Schedule;
  template: TemplateDto | null;
  openRate: number | null;
  clickThroughRate: number | null;
  segments: { segmentId: string; segmentName: string }[];
}

export interface SMSCampaign extends FormValues {
  id: string;
  status: SMSCampaignStatus;
  scheduleType: ScheduleType;
}

export type CampaignUpsertDto = Omit<
  CampaignDto,
  /* Relationships */
  | "template"
  | "segments"
  /* Server-managed */
  | "openRate"
  | "clickThroughRate"
  | "status"
> & {
  templateId: string | null;
  segmentIds: string[];
};

export enum SendType {
  Once = "Once",
  Continuous = "Continuous",
}

export interface Schedule {
  sendType: SendType;
  startTimestamp: number | null;
  endTimestamp: number | null;
  timezone: string | null;
}

// Responses
export interface CampaignResponse {
  campaign: Campaign;
}

export interface CampaignReport {
  name: string;
  status: CampaignStatus;
  performance: {
    emailsSent: number;
    openRate: number;
    clickThroughRate: number;
    bounced: number;
    unsubscribed: number;
  };
  details: {
    templateImageUrl: string;
    subject: string;
    segments: { segmentId: string; segmentName: string }[];
    scheduledSendTimestamp: number;
    recipients: number;
  };
  stats: {
    links: { linkName: string; uniqueClicks: number; totalClicks: number }[];
    totalOpens: number;
    lastOpenedTimestamp: number | null;
  };
}
export interface CampaignReportResponse {
  campaign: CampaignReport;
}

export interface CampaignsResponse {
  campaigns: Campaign[];
}

export interface CampaignsWithCursorResponse {
  campaigns: CampaignWithCursor[];
}
export interface CheckNameExistsResponse {
  success: true;
  nameExists: boolean;
}

export type FormErrors<T extends Record<string, unknown>> = Partial<
  Record<keyof T, string>
>;

export type SmartScheduleTabMap = {
  [SmartScheduleFormTabs.SMART_SCHEDULE]: CampaignScheduleType.SMART_SCHEDULE;
  [SmartScheduleFormTabs.ALL_AT_ONCE]: CampaignScheduleType.ALL_AT_ONCE;
};
