import { z } from "zod";

// =====================
// BASE SCHEMA
// =====================
export const BasePlatformSchema = z.object({
  mode: z.enum(["simple", "advanced"]).default("simple"),
  language: z.enum(["id", "en"]).default("id"),
  variations: z.union([z.literal(1), z.literal(3), z.literal(5)]).default(1),
  subject: z.string().min(3, "Subjek minimal 3 karakter").max(500),
  style: z.string().max(200).optional(),
  mood: z.string().max(200).optional(),
  negativePrompt: z.string().max(500).optional(),
  referenceImageUrl: z.string().url().optional(),
});

// =====================
// SEEDANCE SCHEMA
// =====================
export const SeedanceSchema = BasePlatformSchema.extend({
  platform: z.literal("seedance"),
  cameraMotion: z
    .enum([
      "static",
      "pan_left",
      "pan_right",
      "tilt_up",
      "tilt_down",
      "zoom_in",
      "zoom_out",
      "dolly_in",
      "dolly_out",
      "orbit",
      "handheld",
      "crane_up",
      "crane_down",
    ])
    .optional(),
  cameraAngle: z
    .enum([
      "eye_level",
      "low_angle",
      "high_angle",
      "bird_eye",
      "worm_eye",
      "dutch_angle",
      "over_shoulder",
    ])
    .optional(),
  lighting: z
    .enum([
      "natural",
      "golden_hour",
      "blue_hour",
      "studio",
      "dramatic",
      "soft",
      "neon",
      "candlelight",
      "backlit",
    ])
    .optional(),
  colorGrading: z
    .enum([
      "cinematic",
      "vintage",
      "vibrant",
      "desaturated",
      "warm",
      "cool",
      "noir",
      "teal_orange",
    ])
    .optional(),
  duration: z.enum(["3s", "5s", "8s", "10s"]).optional(),
  resolution: z.enum(["720p", "1080p", "4K"]).optional(),
  fps: z.enum(["24", "30", "60"]).optional(),
  sceneDescription: z.string().max(1000).optional(),
});

// =====================
// GEMINI OMNI SCHEMA
// =====================
export const GeminiOmniSchema = BasePlatformSchema.extend({
  platform: z.literal("gemini_omni"),
  outputType: z.enum(["image", "video", "audio"]).default("image"),
  quality: z.enum(["standard", "high", "ultra"]).default("high"),
  aspectRatio: z
    .enum(["1:1", "16:9", "9:16", "4:3", "3:4", "21:9"])
    .optional(),
  additionalContext: z.string().max(500).optional(),
  safetyLevel: z.enum(["default", "strict"]).default("default"),
});

// =====================
// KLING MOTION SCHEMA
// =====================
export const KlingMotionSchema = BasePlatformSchema.extend({
  platform: z.literal("kling_motion"),
  motionType: z
    .enum([
      "smooth",
      "dynamic",
      "slow_motion",
      "time_lapse",
      "reverse",
      "loop",
    ])
    .optional(),
  cameraPath: z
    .enum([
      "linear",
      "curved",
      "circular",
      "spiral",
      "figure_8",
      "random",
    ])
    .optional(),
  subjectMotion: z
    .enum([
      "static",
      "walking",
      "running",
      "dancing",
      "floating",
      "rotating",
      "custom",
    ])
    .optional(),
  backgroundMotion: z
    .enum(["static", "parallax", "blur", "zoom", "pan"])
    .optional(),
  transitionType: z
    .enum(["cut", "fade", "dissolve", "wipe", "morph", "none"])
    .optional(),
  motionIntensity: z.enum(["subtle", "moderate", "intense"]).optional(),
});

// =====================
// HIGGSFIELD SCHEMA
// =====================
export const HiggsfieldSchema = BasePlatformSchema.extend({
  platform: z.literal("higgsfield"),
  physicsType: z
    .enum([
      "gravity",
      "fluid",
      "cloth",
      "smoke",
      "fire",
      "explosion",
      "destruction",
    ])
    .optional(),
  particleEffect: z
    .enum([
      "none",
      "sparkles",
      "dust",
      "rain",
      "snow",
      "petals",
      "embers",
      "bubbles",
    ])
    .optional(),
  fluidDynamics: z
    .enum(["none", "water", "lava", "oil", "plasma", "mist"])
    .optional(),
  environmentType: z
    .enum([
      "outdoor",
      "indoor",
      "underwater",
      "space",
      "abstract",
      "dystopian",
    ])
    .optional(),
  cinematicStyle: z
    .enum([
      "blockbuster",
      "indie",
      "documentary",
      "music_video",
      "commercial",
      "art_film",
    ])
    .optional(),
  colorPalette: z.string().max(200).optional(),
});

// =====================
// TIKTOK UGC SCHEMA
// =====================
export const TikTokUGCSchema = BasePlatformSchema.extend({
  platform: z.literal("tiktok_ugc"),
  productName: z.string().max(200).optional(),
  targetAudience: z.string().max(300).optional(),
  hookType: z
    .enum([
      "question",
      "shock",
      "story",
      "benefit",
      "challenge",
      "trend",
      "curiosity",
    ])
    .optional(),
  ctaType: z
    .enum([
      "link_in_bio",
      "comment",
      "share",
      "follow",
      "purchase",
      "download",
    ])
    .optional(),
  videoDuration: z.enum(["15s", "30s", "60s"]).default("30s"),
  ugcPersona: z
    .enum([
      "everyday_user",
      "expert",
      "influencer",
      "skeptic_converted",
      "mom",
      "student",
    ])
    .optional(),
  painPoint: z.string().max(300).optional(),
  benefit: z.string().max(300).optional(),
  trendReference: z.string().max(200).optional(),
});

// =====================
// DISCRIMINATED UNION
// =====================
export const UniversalPromptSchema = z.discriminatedUnion("platform", [
  SeedanceSchema,
  GeminiOmniSchema,
  KlingMotionSchema,
  HiggsfieldSchema,
  TikTokUGCSchema,
]);

// =====================
// OUTPUT SCHEMAS
// =====================
export const ShotListItemSchema = z.object({
  shotNumber: z.number(),
  shotType: z.string(),
  cameraAngle: z.string(),
  cameraMovement: z.string(),
  subject: z.string(),
  action: z.string(),
  duration: z.string(),
  notes: z.string().optional(),
});

export const PromptVariationSchema = z.object({
  index: z.number(),
  textPrompt: z.string(),
  negativePrompt: z.string(),
  jsonOutput: z.record(z.unknown()),
  shotList: z.array(ShotListItemSchema).optional(),
  script: z.string().optional(),
});

export const GeneratorOutputSchema = z.object({
  platform: z.enum([
    "seedance",
    "gemini_omni",
    "kling_motion",
    "higgsfield",
    "tiktok_ugc",
  ]),
  variations: z.array(PromptVariationSchema),
  metadata: z.object({
    generatedAt: z.string(),
    model: z.string(),
    inputLanguage: z.string(),
  }),
});

// =====================
// API REQUEST SCHEMA
// =====================
// Fix #1: variationCount dihapus — pakai input.variations.
// Fix outputFormats: field ini tidak pernah dibaca di route (server selalu
// return semua format: text, json, shotList, script). Dihapus untuk
// menghilangkan dead field yang menyesatkan. Output format filtering
// sepenuhnya ditangani di sisi client (tab UI di GeneratorOutput.tsx).
export const GenerateRequestSchema = z.object({
  platform: z.enum([
    "seedance",
    "gemini_omni",
    "kling_motion",
    "higgsfield",
    "tiktok_ugc",
  ]),
  input: UniversalPromptSchema,
});

// Type exports
export type UniversalPromptInput = z.infer<typeof UniversalPromptSchema>;
export type SeedanceInput = z.infer<typeof SeedanceSchema>;
export type GeminiOmniInput = z.infer<typeof GeminiOmniSchema>;
export type KlingMotionInput = z.infer<typeof KlingMotionSchema>;
export type HiggsfieldInput = z.infer<typeof HiggsfieldSchema>;
export type TikTokUGCInput = z.infer<typeof TikTokUGCSchema>;
export type GeneratorOutput = z.infer<typeof GeneratorOutputSchema>;
export type PromptVariation = z.infer<typeof PromptVariationSchema>;
export type GenerateRequest = z.infer<typeof GenerateRequestSchema>;
export type ShotListItem = z.infer<typeof ShotListItemSchema>;
// variationCount tidak lagi ada di GenerateRequest — gunakan input.variations langsung.
