// Video interfaces
export interface Video {
  id: number;
  title: string;
  filename: string;
  duration: number;
  fileSize: number;
  width: number;
  height: number;
  orientation: 'H' | 'V';
  producer?: string;
  creator?: string;
  tags?: string[] | null;
  category?: string | null;
  quality?: string | null;
  position?: number | null;
  thumbnailUrl?: string;
  publicDirectories: string[];
  isInDigest: boolean;
  createdAt: string;
  updatedAt: string;
  // Analytics fields
  viewCount?: number;
  uniqueViewers?: number;
  totalWatchTimeSeconds?: number;
  averageWatchTimeSeconds?: number;
  completionRate?: number;
  finishCount?: number;
  // Scheduling fields
  scheduledPublishAt?: string | null;
  scheduledUnpublishAt?: string | null;
  isPublished?: boolean;
  publishedAt?: string | null;
  isShort?: boolean;
  accessLevel?: 'free' | 'member' | 'premium';
  // HLS adaptive bitrate state. Populated when the video has been transcoded
  // and the HLS manifest is ready to serve. Players prefer this over the MP4
  // stream when present.
  hlsStatus?: 'PENDING' | 'PROCESSING' | 'READY' | 'FAILED' | 'SKIPPED' | null;
  hlsManifestUrl?: string | null;
}

// Video Analytics interfaces
export interface VideoAnalytics {
  overview: {
    totalViews: number;
    uniqueViewers: number;
    averageWatchTime: number;
    completionRate: number;
    totalWatchTime: number;
  };
  viewsOverTime: Array<{ date: string; count: number }>;
  topReferrers: Array<{ referer: string; count: number }>;
  registeredViewers: Array<{
    userId: string;
    userName: string | null;
    userEmail: string;
    watchTime: number;
    completed: boolean;
    viewedAt: string;
  }>;
}

export interface PreviewLinkResponse {
  previewUrl: string;
  expiresAt: string;
  expiryHours: number;
}

export interface TopVideosResponse {
  metric: 'views' | 'watchTime';
  videos: Array<{
    id: number;
    title: string | null;
    value: number;
  }>;
}

export interface AnalyticsOverviewResponse {
  totalVideos: number;
  totalViews: number;
  totalWatchTimeSeconds: number;
  averageCompletionRate: number;
}

export interface VideosListResponse {
  videos: Video[];
  total: number;
  limit: number;
  offset: number;
}

export interface VideosListParams {
  search?: string;
  orientation?: 'H' | 'V';
  producers?: string[];
  directoryType?: string;
  subdirectories?: string;
  offset?: number;
  limit?: number;
  isShort?: boolean;
}

// Public media interfaces
export interface SharedMedia {
  id: number;
  videoId: number;
  category: 'videos' | 'curated' | 'compilations' | 'playback' | 'highlights';
  isLocked: boolean;
  position: number;
  video: Video;
  createdAt: string;
}

export interface SharedMediaListResponse {
  data: SharedMedia[];
  total: number;
  offset: number;
  limit: number;
}

export interface SharedMediaListParams {
  search?: string;
  category?: string;
  isLocked?: boolean;
  sort?: 'recent' | 'popular' | 'most_viewed';
  offset?: number;
  limit?: number;
}

// Job interfaces
export interface Job {
  id: number;
  type: string;
  status: 'pending' | 'queued' | 'running' | 'completed' | 'failed' | 'cancelled';
  progress: number;
  log: string | null;
  createdAt: string;
  startedAt: string | null;
  completedAt: string | null;
  queuePosition?: number | null;
  waitingReason?: string | null;
}

export interface JobsListResponse {
  jobs: Job[];
  total: number;
}

// Directory types
export interface DirectoryType {
  type: string;
  count: number;
  subdirectories?: string[];
}

// Playlist interfaces
export interface PlaylistCreator {
  id: string;
  name: string | null;
  email: string;
}

export interface PlaylistSummary {
  id: number;
  name: string;
  description: string | null;
  isPublic: boolean;
  shareToken?: string | null;
  videoCount: number;
  totalDurationSeconds: number;
  viewCount: number;
  thumbnailUrl: string | null;
  createdAt: string;
  updatedAt: string | null;
  creator: PlaylistCreator;
  isFeatured: boolean;
  featuredPosition: number | null;
  isOwner: boolean;
}

export interface PlaylistVideoItem {
  id: number;
  mediaId: number;
  position: number;
  addedAt: string;
  video: {
    id: number;
    title: string | null;
    filename: string;
    durationSeconds: number | null;
    quality: string | null;
    orientation: string | null;
    thumbnailUrl: string | null;
    viewCount: number;
    isLocked: boolean;
    createdAt: string;
  };
}

export interface PlaylistDetail extends PlaylistSummary {
  videos: PlaylistVideoItem[];
}

export interface PlaylistsListResponse {
  data: PlaylistSummary[];
  total: number;
  limit: number;
  offset: number;
}

export interface CreatePlaylistBody {
  name: string;
  description?: string;
  isPublic?: boolean;
}

export interface UpdatePlaylistBody {
  name?: string;
  description?: string;
  isPublic?: boolean;
}

export interface ReorderPlaylistVideosBody {
  items: Array<{ mediaId: number; position: number }>;
}

// ============================================================================
// PHOTO GALLERY
// ============================================================================

export interface Photo {
  id: number;
  path: string;
  filename: string;
  originalFilename: string | null;
  title: string | null;
  description: string | null;
  producer: string | null;
  creator: string | null;
  tags: string[] | null;
  width: number | null;
  height: number | null;
  orientation: 'H' | 'V' | 'S' | null;
  fileSize: string | null; // BigInt serialized as string
  format: string | null;
  colorSpace: string | null;
  hasAlpha: boolean | null;
  dpi: number | null;
  cameraMake: string | null;
  cameraModel: string | null;
  focalLength: string | null;
  aperture: string | null;
  shutterSpeed: string | null;
  iso: number | null;
  takenAt: string | null;
  gpsLatitude: number | null;
  gpsLongitude: number | null;
  thumbnailPath: string | null;
  thumbnailUrl: string | null;
  isPublished: boolean;
  publishedAt: string | null;
  category: string | null;
  accessLevel: string;
  position: number | null;
  isLocked: boolean;
  viewCount: number;
  upvoteCount: number;
  commentCount: number;
  albumId: number | null;
  albumPosition: number | null;
  uploaderId: string | null;
  createdAt: string;
  album?: { id: number; title: string } | null;
  uploader?: { id: string; name: string | null; email: string } | null;
}

export interface PhotoAlbum {
  id: number;
  title: string;
  description: string | null;
  coverPhotoId: number | null;
  isPublished: boolean;
  publishedAt: string | null;
  category: string | null;
  accessLevel: string;
  position: number | null;
  isLocked: boolean;
  viewCount: number;
  upvoteCount: number;
  photoCount: number;
  creatorId: string | null;
  createdAt: string;
  updatedAt: string;
  coverPhoto?: { id: number; thumbnailPath: string | null } | null;
  coverThumbnailUrl?: string | null;
  creator?: { id: string; name: string | null; email: string } | null;
  photos?: PhotoAlbumItem[];
  _count?: { photos: number };
}

export interface PhotoAlbumItem {
  id: number;
  title: string | null;
  originalFilename: string | null;
  thumbnailPath: string | null;
  thumbnailUrl: string | null;
  width: number | null;
  height: number | null;
  orientation: string | null;
  format: string | null;
  fileSize: string | null;
  albumPosition: number | null;
  isPublished: boolean;
  createdAt: string;
}

export interface PhotosListResponse {
  photos: Photo[];
  total: number;
  limit: number;
  offset: number;
}

export interface PublicPhoto {
  id: number;
  title: string | null;
  width: number | null;
  height: number | null;
  orientation: string | null;
  format: string | null;
  producer: string | null;
  category: string | null;
  publishedAt: string | null;
  viewCount: number;
  upvoteCount: number;
  commentCount: number;
  thumbnailUrl: string;
  imageUrl: string;
  albumId: number | null;
}

export interface PublicAlbum {
  id: number;
  title: string;
  description: string | null;
  category: string | null;
  photoCount: number;
  viewCount: number;
  upvoteCount: number;
  publishedAt: string | null;
  coverThumbnailUrl: string | null;
  coverPhoto?: { id: number; width: number | null; height: number | null } | null;
}

export type GalleryItemType = 'video' | 'photo' | 'album';

export interface GalleryItem {
  type: GalleryItemType;
  data: any;
}
