// Types de base pour l'API
export interface User {
  id: string;
  username: string;
  email: string;
  created_at: string;
  updated_at: string;
  is_2fa_enabled?: boolean;
}

export interface Message {
  id: string;
  conversation_id: string;
  sender_id: string;
  content: string;
  message_type: 'text' | 'image' | 'audio' | 'file';
  attachment_url?: string;
  created_at: string;
  updated_at: string;
  sender?: User;
}

export interface Conversation {
  id: string;
  name: string;
  type: 'direct' | 'group';
  creator_id: string;
  created_at: string;
  updated_at: string;
  participants?: User[];
  last_message?: Message;
  unread_count?: number;
}

export interface Track {
  id: string;
  title: string;
  artist_id: string;
  album_id?: string;
  genre: string;
  file_path: string;
  cover_art_url?: string;
  duration: number;
  plays: number;
  created_at: string;
  updated_at: string;
  artist?: User;
}

export interface Session {
  id: string;
  user_id: string;
  token: string;
  created_at: string;
  expires_at: string;
  ip_address: string;
  user_agent: string;
  is_valid: boolean;
  revoked_at?: string;
}

export interface AuditLog {
  id: string;
  user_id?: string;
  action: string;
  entity_type?: string;
  entity_id?: string;
  ip_address?: string;
  user_agent?: string;
  details?: Record<string, any>;
  created_at: string;
}

// Types pour les requêtes d'authentification
export interface LoginRequest {
  email: string;
  password: string;
}

export interface RegisterRequest {
  username: string;
  email: string;
  password: string;
}

export interface AuthResult {
  access_token: string;
  refresh_token: string;
  expires_at: number;
  user_id: string;
  user: User;
}

export interface TokenClaims {
  user_id: string;
  exp: number;
  iat: number;
}

// Types pour les requêtes utilisateur
export interface CreateUserRequest {
  username: string;
  email: string;
  password: string;
}

export interface UpdateUserRequest {
  username?: string;
  email?: string;
  password?: string;
}

export interface UpdateProfileRequest {
  username?: string;
  bio?: string;
  avatar_url?: string;
}

// Types pour les requêtes de messages
export interface SendMessageRequest {
  conversation_id: string;
  content: string;
  message_type?: 'text' | 'image' | 'audio' | 'file';
  attachment_url?: string;
}

export interface UpdateMessageRequest {
  content?: string;
}

// Types pour les requêtes de conversations
export interface CreateConversationRequest {
  name: string;
  type: 'direct' | 'group';
  participant_ids: string[];
}

export interface UpdateConversationRequest {
  name?: string;
}

// Types pour les requêtes de tracks
export interface UploadTrackRequest {
  title: string;
  artist_id: string;
  album_id?: string;
  genre: string;
  file: File;
  cover_art?: File;
}

export interface UpdateTrackRequest {
  title?: string;
  artist_id?: string;
  album_id?: string;
  genre?: string;
}

// Types pour la pagination
export interface PaginationParams {
  page?: number;
  limit?: number;
  cursor?: string;
}

export interface PaginationData {
  page: number;
  limit: number;
  total: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
  next_cursor?: string;
  prev_cursor?: string;
}

// Types pour les réponses API
export interface ApiResponse<T> {
  data: T;
  message?: string;
  success: boolean;
}

export interface ApiError {
  code: string;
  message: string;
  status: number;
  details?: Record<string, any>;
}

export interface ListResponse<T> {
  items: T[];
  pagination: PaginationData;
}

// Types pour les requêtes de liste
export interface ListUsersRequest extends PaginationParams {
  query?: string;
}

export interface ListMessagesRequest extends PaginationParams {
  conversation_id: string;
}

export interface ListConversationsRequest extends PaginationParams {
  query?: string;
}

export interface ListTracksRequest extends PaginationParams {
  artist?: string;
  genre?: string;
}

export interface SearchTracksRequest extends PaginationParams {
  query: string;
}

// Types pour les réponses de liste
export interface ListUsersResponse extends ListResponse<User> {}
export interface ListMessagesResponse extends ListResponse<Message> {}
export interface ListConversationsResponse extends ListResponse<Conversation> {}
export interface ListTracksResponse extends ListResponse<Track> {}
export interface SearchTracksResponse extends ListResponse<Track> {}

// Types pour les uploads
export interface UploadFileRequest {
  file: File;
  type: 'image' | 'audio' | 'document';
}

export interface UploadFileResponse {
  file_id: string;
  file_name: string;
  file_url: string;
  mime_type: string;
  size: number;
}

export interface FileMetadata {
  id: string;
  file_name: string;
  file_url: string;
  mime_type: string;
  size: number;
  uploader_id: string;
  uploaded_at: string;
}

// Types pour les notifications
export interface Notification {
  id: string;
  user_id: string;
  type: 'new_message' | 'track_uploaded' | 'user_mentioned' | 'system';
  content: string;
  read: boolean;
  created_at: string;
}

// Types pour les WebSockets
export interface WebSocketMessage {
  type: 'message' | 'typing' | 'read_receipt' | 'user_joined' | 'user_left' | 'error';
  data: any;
  timestamp: string;
}

export interface TypingIndicator {
  user_id: string;
  conversation_id: string;
  is_typing: boolean;
}

export interface ReadReceipt {
  user_id: string;
  message_id: string;
  read_at: string;
}

// Types pour les erreurs
export interface ValidationError {
  field: string;
  message: string;
  code: string;
}

export interface FormError {
  message: string;
  errors?: ValidationError[];
}

// Types pour les états de l'application
export interface AppState {
  user: User | null;
  isAuthenticated: boolean;
  isLoading: boolean;
  error: string | null;
}

export interface ChatState {
  conversations: Conversation[];
  activeConversation: string | null;
  messages: Record<string, Message[]>;
  typingUsers: Record<string, TypingIndicator[]>;
  unreadCounts: Record<string, number>;
}

export interface LibraryState {
  tracks: Track[];
  playlists: Playlist[];
  currentTrack: Track | null;
  isPlaying: boolean;
  volume: number;
  currentTime: number;
  duration: number;
}

export interface Playlist {
  id: string;
  name: string;
  description?: string;
  user_id: string;
  track_ids: string[];
  created_at: string;
  updated_at: string;
  tracks?: Track[];
}

// Types pour les thèmes
export interface Theme {
  name: string;
  colors: {
    primary: string;
    secondary: string;
    background: string;
    surface: string;
    text: string;
    textSecondary: string;
    border: string;
    error: string;
    warning: string;
    success: string;
  };
}

// Types pour la configuration
export interface AppConfig {
  apiUrl: string;
  wsUrl: string;
  uploadUrl: string;
  maxFileSize: number;
  supportedFormats: string[];
  features: {
    chat: boolean;
    library: boolean;
    streaming: boolean;
    uploads: boolean;
    notifications: boolean;
  };
}

// Types utilitaires
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export type Required<T, K extends keyof T> = T & Required<Pick<T, K>>;
export type DeepPartial<T> = {
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

// Types pour les événements
export interface AppEvent {
  type: string;
  payload: any;
  timestamp: number;
}

export interface EventHandler<T = any> {
  (event: AppEvent & { payload: T }): void;
}
