import { z } from "zod";

// ─── Login ────────────────────────────────────────────────────────────────────
export interface LoginResponse {
  email: string;
  token: string;
}

// ─── Calendar Sync Settings ─────────────────────────────────────────────────
export interface UpdateCalendarSyncSettingRequest {
  enabled: boolean;
}

export interface UpdateCalendarSyncSettingResponse {
  success: boolean;
  message: string;
  data: {
    calendar_sync_enabled: boolean;
  };
}

export interface CalendarSyncSettingStatusResponse {
  success: boolean;
  message: string;
  data: {
    calendar_sync_enabled: boolean;
  };
}

// ─── Add Meeting ──────────────────────────────────────────────────────────────
export const addMeetingSchema = z.object({
  meet_link: z
    .string()
    .min(1, "Meeting link is required")
    .url("Must be a valid URL")
    .refine(
      (val) =>
        val.startsWith("https://meet.google.com/") ||
        val.includes("teams.microsoft.com/l/meetup-join") ||
        val.includes("teams.live.com/meet/") ||
        val.includes("zoom.us/j/") ||
        val.includes("zoom.us/my/") ||
        val.includes("zoom.us/s/") ||
        val.includes("zoom.us/wc/") ||
        val.includes("meet.jit.si/"),
      "Must be a valid Google Meet, Microsoft Teams, Zoom, or Jitsi link"
    ),
  title: z
    .string()
    .min(3, "Title must be at least 3 characters")
    .max(100, "Title must be under 100 characters")
    .regex(/\S/, "Title cannot be only whitespace"),
});

export type AddMeetingRequest = z.infer<typeof addMeetingSchema>;

export interface AddMeetingResponseData {
  meeting_id: number;
  meet_link: string;
  title: string;
  platform?: string;       // e.g. "google_meet", "microsoft_teams", "zoom", "jitsi"
  scheduled_at?: string | null;
  status?: string;
}

export interface AddMeetingResponse {
  success: boolean;
  message: string;
  data: AddMeetingResponseData;
}

// ─── List Meetings ───────────────────────────────────────────────────────────
export interface RecordingDetails {
  started_at: string | null;
  stopped_at: string | null;
  duration_seconds: number;
  duration_formatted: string;
}

export interface ParticipantsDetails {
  count: number;
  names: string[];
}

export interface ListMeetingsSummary {
  recorded_meetings_count: number;
  total_recorded_duration_seconds: number;
  total_recorded_duration_formatted: string;
}

export interface ListMeetingItem {
  meeting_id: number | null;
  title: string;
  meet_link: string;
  platform?: string;
  status: string;
  status_segment?: string;
  created_at: string;
  scheduled_at?: string | null;
  recall_join_at?: string | null;
  participants_details?: ParticipantsDetails;
  recording_details?: RecordingDetails;
  app_toggle_enabled?: boolean | null;
  recurring_candidate_id?: number;
  candidate_id?: number | null;
  is_recurring?: boolean;
}


export interface ListMeetingsPagination {
  page: number;
  limit: number;
  total: number;
  total_pages: number;
  has_next?: boolean;
  next_page?: number | null;
}

export interface ListMeetingsSegmentCount {
  segment: string;
  total_count: number;
}

export interface ListMeetingsResponse {
  success: boolean;
  pagination: ListMeetingsPagination;
  summary?: ListMeetingsSummary;
  segment_counts?: Record<string, number>;
  status_counts?: Record<string, number>;
  segments?: ListMeetingsSegmentCount[];
  data: ListMeetingItem[];
}

export interface ListMeetingsParams {
  page?: number;
  limit?: number;
  status?: string;
  search?: string;
  platform?: string;
  type?: string;
  time?: string;
  sort_by?: string;
}

export type ListMeetingsApiResponse = ListMeetingItem[] | ListMeetingsResponse;

// ─── Start Bot ───────────────────────────────────────────────────────────────
export interface StartBotResponse {
  success: boolean;
  message: string;
  data: {
    meeting_id: number;
    recall_bot_id: string;
    status: string;
    bot_status: string;
    provider: string;
  };
}

// ─── Stop Recording ──────────────────────────────────────────────────────────
export interface StopRecordingResponse {
  success: boolean;
  message: string;
  data: {
    meeting_id: number;
    stopped_at?: string;
    file_path?: string;
  };
}

// ─── Transcript View (recall.ai) ──────────────────────────────────────────────────
export interface TranscriptViewSegment {
  index: number;
  speaker: string;
  text: string;
  start: number;
  end: number;
}

export interface TranscriptViewRecording {
  audio_download_url: string | null;
  video_download_url: string | null;
  transcript_download_url: string | null;
  transcript_provider_data_download_url: string | null;
}

export interface TranscriptViewData {
  meeting_id: number;
  transcript_id: number;
  recall_transcript_id: string;
  status: string;
  participants: string[];
  speaker_map: Record<string, string>;
  segments: TranscriptViewSegment[];
  full_text: string;
  download_url: string | null;
  provider_data_download_url: string | null;
  video_download_url?: string | null;
  recording: TranscriptViewRecording | null;
}

export type TranscriptViewResponse =
  | {
      success: true;
      message?: string;
      data: TranscriptViewData;
    }
  | {
      success: false;
      message?: string;
      data: Record<string, never>;
    };

// ─── Transcript Status ────────────────────────────────────────────────────────
export interface TranscriptSegmentAPI {
  speaker: string;
  speaker_label: string;
  text: string;
  start: number;
  end: number;
  start_formatted: string;
  end_formatted: string;
}

export interface AudioRecording {
  recording_id: number;
  status: string;
  file_name: string;
  file_path: string;
  content_type: string;
  available: boolean;
  download_url: string;       // backend proxy URL (old flow)
  direct_url?: string | null; // pre-signed S3 URL (recall.ai flow — no auth header needed)
  download_direct_url?: string | null; // pre-signed S3 URL for download action
  download_file_name?: string;
  download_content_type?: string;
}

export interface TranscriptStatusData {
  transcript_id: number;
  meeting_id: number;
  status: "pending" | "processing" | "done" | "failed" | "too_short";
  language: string;
  duration_seconds: number;
  error_message: string | null;
  created_at: string;
  completed_at: string | null;
  participants: string[];
  speaker_map: Record<string, string>;
  transcript: TranscriptSegmentAPI[];
  full_text: string;
  audio?: AudioRecording;
}

export interface TranscriptStatusResponse {
  success: boolean;
  data: TranscriptStatusData;
}

// ─── Transcribe File (direct upload) ────────────────────────────────────────
export interface TranscribeFileResponse {
  success: boolean;
  data: {
    status: string;
    language: string;
    duration_seconds: number;
    participants: string[];
    speaker_map: Record<string, string>;
    transcript: TranscriptSegmentAPI[];
    full_text: string;
  };
}

// ─── Upload Audio ────────────────────────────────────────────────────────────
export interface UploadAudioResponse {
  success: boolean;
  message: string;
  data: {
    meeting_id: number;
    title: string;
    status: string;
  };
}

// ─── Create Scheduled Meeting ───────────────────────────────────────────────
export interface CreateScheduledMeetingRequest {
  title: string;
  start_time: string;  // ISO 8601 with timezone offset
  end_time: string;    // ISO 8601 with timezone offset
  description?: string;
  auto_schedule_bot: boolean;
}

export interface CreateScheduledMeetingResponseData {
  meeting_id: number;
  title: string;
  meet_link: string;
  scheduled_at: string;
  calendar_event_id: string;
  recall_bot_id: string;
  status: string;
  bot_status: string;
}

export interface CreateScheduledMeetingResponse {
  success: boolean;
  message: string;
  data: CreateScheduledMeetingResponseData;
}

// ─── Abort Meeting ────────────────────────────────────────────────────────
export interface AbortMeetingResponse {
  success: boolean;
  message: string;
  data: {
    meeting_id: number;
    status: string;
  };
}

// ─── Generic API error ────────────────────────────────────────────────────────
export interface ApiError {
  message: string;
  status: number;
}

// ─── Invited Meetings ────────────────────────────────────────────────────────
export interface InvitedMeetingItem {
  invite_id: number;
  host_user_id: number;
  recurring_series_id: string | null;
  google_event_id: string | null;
  title: string;
  meet_link: string;
  platform: string;
  scheduled_at: string;
  guest_rsvp_status: string;
  organizer_email: string;
  is_recurring: boolean;
  status?: string;
  meeting_id?: number;
  meeting_status?: string;
  has_meeting_access?: boolean;
  recording_status?: string;
  transcript_status?: string;
  has_recording?: boolean;
  has_transcript?: boolean;
}

export interface InvitedMeetingsResponse {
  success: boolean;
  pagination?: ListMeetingsPagination;
  summary?: ListMeetingsSummary;
  segment_counts?: Record<string, number>;
  status_counts?: Record<string, number>;
  data: InvitedMeetingItem[];
}

// ─── Recurring Candidate Decision ───────────────────────────────────────────
export interface RecurringCandidateDecisionRequest {
  action: "on" | "off";
}

export interface RecurringCandidateDecisionResponseData {
  candidate_id: number;
  meeting_id: number;
  host_rsvp_status: string;
  app_toggle_enabled: boolean;
  bot_permission_status: string;
  bot_created: boolean;
  recall_bot_id: string;
  bot_join_mode: string;
  reason: string | null;
}

export interface RecurringCandidateDecisionResponse {
  success: boolean;
  message: string;
  data: RecurringCandidateDecisionResponseData;
}

