/**
 * 项目类型定义
 * ============
 *
 * 本项目持久化数据的唯一真相源。
 * 所有需要保存到 meta.json 的字段类型都在这里定义。
 */

import type { AlignmentResult } from '../api/types';

// ------------------------------------------------------------------
// ScriptShot — 分镜/空镜（扩展后端类型，添加本地运行时字段）
// ------------------------------------------------------------------

export interface ScriptShot {
  id: number;
  type: 'segment' | 'empty_shot';
  scene?: string;
  voiceover: string;
  duration: string;
  videoPath?: string;
  videoUrl?: string;
  elementId?: number;
  voiceId?: string;
  voiceVolume?: number;
  bgmVolume?: number;
  alignmentResult?: AlignmentResult;
  burnedVideoPath?: string;
  burnedAt?: number;
  audioPath?: string;
  audioUrl?: string;
  audioStartTime?: number;
  audioEndTime?: number;
  actualDuration?: number;
  clipAudioPath?: string;
  clipAudioUrl?: string;
  lipSyncTaskId?: string;
  lipSyncState?: string;
  lipSyncVideoPath?: string;
  lipSyncVideoUrl?: string;
  lipSyncStartTime?: number;
  clipVideoPath?: string;
  emptyShotMaterial?: { url: string; duration: number };
}

// ------------------------------------------------------------------
// CoverConfig — 封面配置
// ------------------------------------------------------------------

interface CoverConfig {
  caption?: string;
  coverStyle?: {
    fontSize: number;
    color: string;
    strokeColor: string;
    strokeWidth: number;
  };
  selectedPreset?: string;
  bgSource?: 'first-frame' | 'ai-generate';
  template?: 'dual-title';
  backgroundImage?: string | null;
  mainTitle?: string;
  subtitle?: string;
  tags?: string;
  /** 封面形象图片 URL（抠图后的透明背景人物照） */
  avatarImage?: string | null;
}

// ------------------------------------------------------------------
// ProjectMeta — 项目元数据（磁盘存储的权威格式）
// ------------------------------------------------------------------

export interface ProjectMeta {
  // === 系统字段（自动管理）===
  id: string;
  title: string;
  status: 'draft' | 'published';
  currentStep: number;
  createdAt: number;
  updatedAt: number;
  /** 数据格式版本，用于未来迁移 */
  version?: number;

  // === 创作参数 ===
  topic?: string;
  categoryCode?: string;
  filename?: string;

  // === 素材选择 ===
  selectedVoiceId?: string;
  avatarMaterialPath?: string;
  avatarMaterialName?: string;
  avatarMaterialDuration?: number;

  // === 配音 ===
  dubbingAudioUrl?: string;
  dubbingAudioPath?: string;
  dubbingAudioDuration?: number;
  voiceSpeed?: number;
  voiceVolume?: number;
  voicePitch?: number;

  // === 字幕 ===
  subtitleAlignment?: AlignmentResult;
  burnedVideoPath?: string;
  mainTitle?: string;
  subTitle?: string;
  mainTitlePreset?: string;
  subTitlePreset?: string;
  captionPreset?: string;

  // === 视频生成 ===
  composedVideoUrl?: string;
  composedVideoPath?: string;
  lipSyncTaskId?: string;
  lipSyncState?: string;
  lipSyncedVideoPath?: string;
  lipSyncedVideoUrl?: string;

  // === 封面 ===
  coverPath?: string;
  coverConfig?: CoverConfig;

  // === BGM ===
  bgmMusicId?: number;
  bgmMusicTitle?: string;
  bgmMusicPath?: string;
  bgmVolume?: number;

  // === 成品 ===
  finalVideoPath?: string;
  finalVideoDuration?: number;
  exportedAt?: number;

  // === 素材 ===
  userUploadedMaterials?: Record<string, { path: string; duration: number }>;

  // === 步骤脏标记（上游更新后下游需重新生成）===
  stepDirtyFlags?: Record<number, boolean>;
}

// ------------------------------------------------------------------
// ProjectState — Zustand Store 状态（扩展 ProjectMeta）
// ------------------------------------------------------------------

export interface ProjectState extends ProjectMeta {
  segments: ScriptShot[];
  _isLoading: boolean;
  _hasHydrated: boolean;
  // 以下字段在 ProjectMeta 中为可选，但在 Store 中始终有默认值，故覆盖为必需
  voiceSpeed: number;
  voiceVolume: number;
  voicePitch: number;
}

// ------------------------------------------------------------------
// MetaOverrides — 允许通过 saveMetaToLocalFile 覆盖的字段
// ------------------------------------------------------------------

export type MetaOverrides = Partial<
  Omit<ProjectMeta, 'id' | 'status' | 'createdAt' | 'updatedAt' | 'version'>
>;

export type ShotType = ScriptShot['type'];
