import { fetchWithAuth } from "./client";

// ── 类型定义 ────────────────────────────────────────────────────────────────────

export interface FormatFeatures {
  supports_markdown: boolean;
  supports_html: boolean;
  max_heading_level: number;
  supports_code_block: boolean;
  supports_emoji: boolean;
}

export interface PlatformInfo {
  id: string;
  name: string;
  icon: string;
  max_title_length: number;
  max_content_length: number;
  min_content_length: number;
  supported_media: string[];
  max_images: number;
  best_publish_times: string[];
  best_publish_days: string[];
  format_features: FormatFeatures | null;
  rules: string[];
  seo_tips: string[];
}

export interface PlatformListResponse {
  platforms: PlatformInfo[];
}

export interface ValidationIssue {
  severity: "high" | "medium" | "low";
  message: string;
}

export interface ContentValidateResponse {
  is_valid: boolean;
  score: number;
  issues: ValidationIssue[];
  passed: string[];
}

export interface ScheduleItem {
  platform: string;
  platform_name: string;
  suggested_time: string;
  reason: string;
}

export interface PublishStrategyResponse {
  schedule: ScheduleItem[];
  tags: Record<string, string[]>;
  tips: string[];
}

export interface ContentFormatResponse {
  platform: string;
  formatted_content: string;
  original_length: number;
  formatted_length: number;
}

export interface PlatformSchedule {
  platform: string;
  platform_name: string;
  scheduled_time: string;
  status: string;
}

export interface ScheduleCreateResponse {
  schedule_id: string;
  content_title: string;
  platforms: PlatformSchedule[];
  tips: string[];
  created_at: string;
}

// ── API ────────────────────────────────────────────────────────────────────────

export const distributionApi = {
  getPlatforms: (token?: string) =>
    fetchWithAuth("/api/v1/distribution/platforms", {}, token) as Promise<PlatformListResponse>,

  validateContent: (
    token?: string,
    data?: { content: string; title: string; platform: string }
  ) =>
    fetchWithAuth(
      "/api/v1/distribution/validate",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<ContentValidateResponse>,

  generateStrategy: (
    token?: string,
    data?: { content_title: string; platforms: string[]; industry?: string }
  ) =>
    fetchWithAuth(
      "/api/v1/distribution/strategy",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<PublishStrategyResponse>,

  formatContent: (token?: string, data?: { content: string; platform: string }) =>
    fetchWithAuth(
      "/api/v1/distribution/format",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<ContentFormatResponse>,

  createSchedule: (
    token?: string,
    data?: {
      content_title: string;
      content_id?: string;
      platforms: string[];
      industry?: string;
      scheduled_times?: Record<string, string>;
    }
  ) =>
    fetchWithAuth(
      "/api/v1/distribution/schedule",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<ScheduleCreateResponse>,
};
