// ============================================
// Cat Tv - TypeScript Interfaces & Types
// ============================================

// --- Auth ---
export interface LoginCredentials {
  email: string;
  password: string;
}

export interface AuthResponse {
  accessToken: string;
  refreshToken: string;
  user: User;
}

export interface TokenPayload {
  sub: string;
  email: string;
  role: UserRole;
  iat: number;
  exp: number;
}

// --- User ---
export type UserRole = 'admin' | 'user' | 'moderator';
export type UserStatus = 'active' | 'suspended' | 'expired';

export interface User {
  id: string;
  username: string;
  email: string;
  role: UserRole;
  status: UserStatus;
  avatarUrl?: string;
  createdAt: string;
  expiresAt?: string;
  lastLogin?: string;
  maxDevices: number;
  connectedDevices: number;
}

export interface UserProfile extends User {
  devices: ConnectedDevice[];
  favorites: Favorite[];
  watchHistory: HistoryItem[];
}

export interface ConnectedDevice {
  id: string;
  name: string;
  type: 'web' | 'mobile' | 'tv' | 'tablet';
  lastActive: string;
  ip: string;
  current: boolean;
}

// --- Content ---
export interface Category {
  id: string;
  name: string;
  slug: string;
  icon?: string;
  count?: number;
}

export interface Channel {
  id: string;
  name: string;
  number?: number;
  logoUrl: string;
  streamUrl: string;
  category: Category;
  categoryId: string;
  isLive: boolean;
  currentProgram?: string;
  description?: string;
  viewers?: number;
  isFavorite?: boolean;
}

export interface Movie {
  id: string;
  title: string;
  originalTitle?: string;
  synopsis: string;
  year: number;
  duration: number; // minutes
  rating: number; // 0-10
  posterUrl: string;
  backdropUrl: string;
  trailerUrl?: string;
  streamUrl: string;
  genres: string[];
  category: Category;
  categoryId: string;
  director?: string;
  cast?: string[];
  language?: string;
  subtitles?: string[];
  quality?: '720p' | '1080p' | '4K';
  isFavorite?: boolean;
  views?: number;
  addedAt: string;
}

export interface Series {
  id: string;
  title: string;
  originalTitle?: string;
  synopsis: string;
  year: number;
  endYear?: number;
  rating: number;
  posterUrl: string;
  backdropUrl: string;
  trailerUrl?: string;
  genres: string[];
  category: Category;
  categoryId: string;
  creator?: string;
  cast?: string[];
  seasons: Season[];
  totalSeasons: number;
  totalEpisodes: number;
  status: 'ongoing' | 'ended' | 'cancelled';
  isFavorite?: boolean;
  views?: number;
  addedAt: string;
}

export interface Season {
  id: string;
  seriesId: string;
  number: number;
  title?: string;
  year?: number;
  episodes: Episode[];
  episodeCount: number;
}

export interface Episode {
  id: string;
  seasonId: string;
  seriesId: string;
  number: number;
  title: string;
  synopsis?: string;
  duration: number; // minutes
  thumbnailUrl?: string;
  streamUrl: string;
  airDate?: string;
  watched?: boolean;
  progress?: number; // 0-100
}

// --- Favorites & History ---
export type FavoriteType = 'channel' | 'movie' | 'series';

export interface Favorite {
  id: string;
  userId: string;
  type: FavoriteType;
  contentId: string;
  content?: Channel | Movie | Series;
  createdAt: string;
}

export interface HistoryItem {
  id: string;
  userId: string;
  type: 'movie' | 'episode' | 'channel';
  contentId: string;
  content?: Movie | Episode | Channel;
  progress: number; // percentage 0-100
  duration: number; // seconds watched
  lastWatched: string;
  title: string;
  thumbnailUrl?: string;
}

// --- API ---
export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

export interface PaginatedResponse<T> {
  success: boolean;
  data: T[];
  pagination: Pagination;
}

export interface Pagination {
  page: number;
  limit: number;
  total: number;
  totalPages: number;
  hasNext: boolean;
  hasPrev: boolean;
}

export interface ApiError {
  success: false;
  message: string;
  statusCode: number;
  errors?: Record<string, string[]>;
}

// --- Search ---
export interface SearchResult {
  channels: Channel[];
  movies: Movie[];
  series: Series[];
  total: number;
}

export type SearchFilter = 'all' | 'channels' | 'movies' | 'series';

// --- Admin ---
export interface AdminStats {
  totalUsers: number;
  activeUsers: number;
  totalChannels: number;
  activeChannels: number;
  totalMovies: number;
  totalSeries: number;
  onlineUsers: number;
  totalViews: number;
  recentActivity: ActivityItem[];
}

export interface ActivityItem {
  id: string;
  type: 'user_login' | 'content_added' | 'user_registered' | 'content_updated';
  description: string;
  timestamp: string;
  userId?: string;
  username?: string;
}

export interface CreateUserPayload {
  username: string;
  email: string;
  password: string;
  role: UserRole;
  expiresAt?: string;
  maxDevices?: number;
}

export interface UpdateUserPayload {
  username?: string;
  email?: string;
  role?: UserRole;
  status?: UserStatus;
  expiresAt?: string;
  maxDevices?: number;
}

export interface CreateChannelPayload {
  name: string;
  logoUrl: string;
  streamUrl: string;
  categoryId: string;
  number?: number;
  description?: string;
}

export interface CreateMoviePayload {
  title: string;
  synopsis: string;
  year: number;
  duration: number;
  rating: number;
  posterUrl: string;
  backdropUrl: string;
  streamUrl: string;
  genres: string[];
  categoryId: string;
  trailerUrl?: string;
  director?: string;
  cast?: string[];
  quality?: '720p' | '1080p' | '4K';
}

export interface CreateSeriesPayload {
  title: string;
  synopsis: string;
  year: number;
  rating: number;
  posterUrl: string;
  backdropUrl: string;
  genres: string[];
  categoryId: string;
  creator?: string;
  cast?: string[];
  status: 'ongoing' | 'ended' | 'cancelled';
}

// --- Player ---
export interface PlayerState {
  isPlaying: boolean;
  currentTime: number;
  duration: number;
  volume: number;
  isMuted: boolean;
  isFullscreen: boolean;
  quality: string;
  playbackSpeed: number;
  isBuffering: boolean;
  error: string | null;
}

// --- UI ---
export interface ToastMessage {
  id: string;
  type: 'success' | 'error' | 'info' | 'warning';
  message: string;
  duration?: number;
}

export interface DropdownOption {
  value: string;
  label: string;
  icon?: React.ReactNode;
  disabled?: boolean;
}

export interface TableColumn<T> {
  key: keyof T | string;
  label: string;
  sortable?: boolean;
  render?: (item: T) => React.ReactNode;
  width?: string;
}
