import type { MediaProbe } from '../media/media.types';

export const AV_PROJECT_SCHEMA_VERSION = 1 as const;

export type SourceStatus = 'attached' | 'missing';

export interface SourceHash {
  algorithm: 'sha-256';
  value: string;
  scope: 'full' | 'partial';
  bytesHashed?: number;
}

export interface MediaAssetReference {
  id: string;
  originalName: string;
  size: number;
  lastModified: number;
  mimeType: string;
  probe?: MediaProbe;
  sourceStatus: SourceStatus;
  hash?: SourceHash;
}

export interface CropSettings {
  x: number;
  y: number;
  width: number;
  height: number;
}

export interface ResizeSettings {
  width?: number;
  height?: number;
  mode: 'fit' | 'fill' | 'stretch';
  allowUpscale?: boolean;
  algorithm?: 'fast_bilinear' | 'bilinear' | 'bicubic' | 'lanczos';
  paddingColor?: string;
}

export interface NormalizationSettings {
  targetLufs: number;
  truePeakDb: number;
  loudnessRangeLufs?: number;
}

export type FadeCurve = 'tri' | 'qsin' | 'exp';

export interface TimelineClipAudioSettings {
  enabled: boolean;
  gainDb?: number;
  normalization?: NormalizationSettings;
  fadeInSeconds?: number;
  fadeOutSeconds?: number;
  fadeCurve?: FadeCurve;
}

export interface TimelineClipVideoSettings {
  enabled: boolean;
  fadeInSeconds?: number;
  fadeOutSeconds?: number;
  fadeCurve?: FadeCurve;
}

export interface TimelineClip {
  id: string;
  assetId: string;
  sourceInSeconds: number;
  sourceOutSeconds: number;
  crop?: CropSettings;
  resize?: ResizeSettings;
  rotation?: 0 | 90 | 180 | 270;
  frameRate?: number;
  audio?: TimelineClipAudioSettings;
  video?: TimelineClipVideoSettings;
}

export interface OutputSettings {
  presetId: string;
  fileName: string;
  container: string;
  videoEnabled: boolean;
  audioEnabled: boolean;
  width?: number;
  height?: number;
  frameRate?: number;
  sampleRate?: number;
  channelLayout?: 'mono' | 'stereo' | '5.1';
  missingAudioPolicy?: 'insert-silence' | 'drop-all' | 'reject';
}

export interface ProjectMetadataSettings {
  title?: string;
  artist?: string;
  album?: string;
  comment?: string;
  custom: Record<string, string>;
}

export interface ProjectChapter {
  id: string;
  startSeconds: number;
  endSeconds: number;
  title: string;
  timeBase?: string;
  metadata?: Record<string, string>;
}

export interface ProjectSubtitleTrack {
  id: string;
  assetId?: string;
  sourceName?: string;
  sourceFormat?: 'srt' | 'vtt' | 'ass';
  language?: string;
  title?: string;
  offsetSeconds?: number;
  default?: boolean;
  forced?: boolean;
  mode: 'exclude' | 'mux' | 'burn-in';
  sourceStatus?: SourceStatus;
}

export interface ProjectUiState {
  selectedAssetId?: string;
  selectedClipId?: string;
  inspectorPanel?: 'media' | 'clip' | 'output';
  timelineZoom?: number;
}

export interface AvProjectV1 {
  schemaVersion: 1;
  id: string;
  name: string;
  createdAt: string;
  updatedAt: string;
  assets: MediaAssetReference[];
  timeline: TimelineClip[];
  output: OutputSettings;
  metadata: ProjectMetadataSettings;
  chapters: ProjectChapter[];
  subtitles: ProjectSubtitleTrack[];
  ui?: ProjectUiState;
}

export type AvProject = AvProjectV1;

export interface CreateProjectOptions {
  id?: string;
  name?: string;
  timestamp?: string;
  output?: Partial<OutputSettings>;
}

export interface BrowserFileIdentity {
  name: string;
  size: number;
  lastModified: number;
  type: string;
}

export interface CreateMediaAssetOptions {
  id?: string;
  probe?: MediaProbe;
  hash?: SourceHash;
  sourceStatus?: SourceStatus;
}

export interface LegacyAvProjectV0 {
  schemaVersion: 0;
  id: string;
  title?: string;
  name?: string;
  createdAt: string;
  updatedAt: string;
  media?: unknown[];
  assets?: unknown[];
  clips?: unknown[];
  timeline?: unknown[];
  output?: unknown;
  metadata?: unknown;
  chapters?: unknown[];
  subtitles?: unknown[];
  ui?: unknown;
}
