import { z } from 'zod';
import { GradialImageSchema, type GradialImage } from './image.js';

// ---------------------------------------------------------------------------
// Video source variant (format alternatives)
// ---------------------------------------------------------------------------

export interface VideoSource {
  src: string;
  type: string; // MIME type: video/mp4, video/webm
}

// ---------------------------------------------------------------------------
// GradialVideo — DAM-backed video asset with compiler-resolved derivatives
// ---------------------------------------------------------------------------

export interface GradialVideo {
  /** Discriminator — always 'gradial.video'. */
  $type: 'gradial.video';
  /** DAM asset identifier. */
  assetId: string;
  /** DAM version identifier. */
  versionId: string;
  /** Accessible description of the video content. */
  alt: string;
  /** Primary video URL (compiler-resolved). */
  src: string;
  /** Primary video MIME type (e.g. 'video/mp4'). */
  type: string;
  /** Intrinsic width in pixels. */
  width: number;
  /** Intrinsic height in pixels. */
  height: number;
  /**
   * Poster frame — a full GradialImage with responsive srcset.
   * The compiler extracts or generates this from the video asset.
   */
  poster?: GradialImage;
  /** Format variants (e.g. mp4 + webm). Maps to <video><source> elements. */
  sources?: VideoSource[];
  /** Duration in seconds (if known). */
  duration?: number;
}

// ---------------------------------------------------------------------------
// Zod schema
// ---------------------------------------------------------------------------

export const VideoSourceSchema = z.object({
  src: z.string().min(1),
  type: z.string().min(1),
});

export const GradialVideoSchema = z.object({
  $type: z.literal('gradial.video'),
  assetId: z.string().min(1),
  versionId: z.string().min(1),
  alt: z.string(),
  src: z.string().min(1),
  type: z.string().min(1),
  width: z.number().int().nonnegative(),
  height: z.number().int().nonnegative(),
  poster: GradialImageSchema.optional(),
  sources: z.array(VideoSourceSchema).optional(),
  duration: z.number().nonnegative().optional(),
});
