import type { ProviderProps, ProviderType } from "./providers";

export const SCHEDULE_FREQUENCY = {
  DAILY: "DAILY",
  INTERVAL: "INTERVAL",
  WEEKLY: "WEEKLY",
  MONTHLY: "MONTHLY",
} as const;

export type ScheduleFrequency =
  (typeof SCHEDULE_FREQUENCY)[keyof typeof SCHEDULE_FREQUENCY];

/** Weekday names indexed by cron convention (0=Sunday). */
export const SCHEDULE_WEEKDAY_LABELS = [
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday",
] as const;

/**
 * Scan-schedule capability modes. In Prowler OSS this is resolved purely from
 * the runtime environment (Cloud vs non-Cloud); the prowler-cloud overlay
 * computes a billing-aware capability and injects it via the `capability` prop.
 *
 * - `ADVANCED`: full scheduling through the new schedules API —
 *   `/schedules/{providerId}` for a single provider, `/schedules/bulk` for the
 *   organization flow (Prowler Cloud, subscribed/paid).
 * - `DAILY_LEGACY`: Prowler OSS / non-Cloud. Only the legacy `Daily` schedule
 *   (`/schedules/daily`) plus optional on-demand scans are allowed.
 * - `MANUAL_ONLY`: Prowler Cloud trial/onboarding. No schedules at all, only a
 *   manual on-demand scan subject to the account quota.
 * - `BLOCKED`: Prowler Cloud account over the scan limit. No scan or schedule
 *   action is available.
 */
export const SCAN_SCHEDULE_CAPABILITY = {
  ADVANCED: "ADVANCED",
  DAILY_LEGACY: "DAILY_LEGACY",
  MANUAL_ONLY: "MANUAL_ONLY",
  BLOCKED: "BLOCKED",
} as const;

export type ScanScheduleCapability =
  (typeof SCAN_SCHEDULE_CAPABILITY)[keyof typeof SCAN_SCHEDULE_CAPABILITY];

export interface ScheduleAttributes {
  scan_enabled: boolean;
  scan_frequency: ScheduleFrequency;
  scan_hour: number | null;
  scan_timezone: string;
  scan_interval_hours: number | null;
  scan_day_of_week: number | null;
  scan_day_of_month: number | null;
  /** Read-only, server-computed next fire time; null when paused/unconfigured. */
  next_scan_at?: string | null;
  /** Read-only, completed_at of the provider's last completed scan. */
  last_scan_at?: string | null;
}

export interface ScheduleRelationships {
  provider: {
    data: {
      type: "providers";
      id: string;
    };
  };
}

export interface ScheduleProps {
  type: "schedules";
  id: string;
  attributes: ScheduleAttributes;
  relationships: ScheduleRelationships;
}

export interface ScheduleApiResponse {
  data: ScheduleProps;
  included?: ProviderProps[];
}

export interface ScheduleUpdatePayload {
  scan_enabled: boolean;
  scan_frequency: ScheduleFrequency;
  scan_hour: number;
  scan_timezone: string;
  scan_interval_hours: number | null;
  scan_day_of_week: number | null;
  scan_day_of_month: number | null;
}

/** Per-provider failure, as returned by `/schedules/bulk`: `{ id, error }`. */
export interface SchedulesBulkFailure {
  id: string;
  error: string;
}

export interface SchedulesBulkAttributes {
  /** Provider ids whose schedule was committed (already excludes failures). */
  updated?: string[];
  failed?: SchedulesBulkFailure[];
}

export interface SchedulesBulkData {
  type: "schedules-bulk";
  id?: string;
  attributes?: SchedulesBulkAttributes;
}

export interface SchedulesBulkResponse {
  data?: SchedulesBulkData;
  error?: unknown;
  errors?: unknown;
  status?: number;
}

/** Minimal provider identity needed to render and target schedule actions. */
export interface ScanScheduleProvider {
  providerId: string;
  providerType: ProviderType;
  providerUid: string;
  providerAlias: string | null;
}

export interface ScheduleFormValues {
  frequency: ScheduleFrequency;
  hour: number;
  dayOfWeek: number;
  dayOfMonth: number;
  intervalHours: number;
  launchInitialScan: boolean;
}
