// Types de base pour l'API
import { TrackStatus } from '@/features/tracks/types/track';
import type { ApiError } from '@/schemas/apiSchemas';
import type { VezaBackendApiInternalModelsTrack, VezaBackendApiInternalModelsTrackStatus, VezaBackendApiInternalModelsUser } from '@/types/generated/api';

export interface PaginatedResponse<T> {
  items: T[];
  total: number;
  page: number;
  limit: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
  next_cursor?: string;
  prev_cursor?: string;
}

export interface UserDTO {
  id: string;
  username: string;
  email: string;
  first_name?: string;
  last_name?: string;
  avatar?: string;
  banner?: string;
  bio?: string;
  location?: string;
  role?: string;
  is_verified?: boolean;
  created_at: string;
}

export interface TrackDTO {
  id: string;
  title: string;
  artist: string | { name: string };
  album?: string;
  genre?: string;
  duration?: number; // seconds
  duration_formatted?: string;
  file_path?: string;
  cover_art_path?: string;
  play_count: number;
  like_count: number;
  is_public: boolean;
  created_at: string;
  updated_at: string;
  creator_id: string;
  status: string; // uploading, processing, completed
  stream_status?: string;
  stream_manifest_url?: string;
  file_size?: number;
  format?: string;
  bitrate?: number;
  sample_rate?: number;
  year?: number;
}

import type { VezaBackendApiInternalModelsUser } from '@/types/generated/api';

/**
 * User type - extends generated type from OpenAPI
 * Action 1.1.3.4: Base type from generated API types
 * Preserves UI-specific computed fields and compatibility
 */
export type User = VezaBackendApiInternalModelsUser & {
  // Required fields (generated type has them as optional, but backend always provides them)
  id: string;
  username: string;
  email: string;
  role: string; // 'user' | 'admin' | 'super_admin'
  is_active: boolean;
  is_verified: boolean;
  is_admin: boolean;
  is_public: boolean;
  created_at: string; // ISO8601 date string
  updated_at: string; // ISO8601 date string
  // Extended properties for UI compatibility
  avatar_url?: string; // Standardize with avatar field if needed (computed from avatar)
  is_2fa_enabled?: boolean;
  banner?: string;
  website?: string;
  social_links?: Record<string, any>; // Extended from generated type's string
  stats?: {
    followers: number;
    following: number;
    tracks: number;
    plays: number;
    followers_count?: number; // Backend compatibility
    following_count?: number; // Backend compatibility
  };
  // Compatibility fields for UI/Mocks
  roles?: string[];
  status?: 'online' | 'offline' | 'away' | 'busy' | 'idle' | 'dnd';
  joinDate?: string;
  tier?: string;
  lastLogin?: string;
  socials?: Record<string, string>;
};

export interface MessageReaction {
  emoji: string;
  user_id: string;
  created_at: string;
}

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;
  reactions?: MessageReaction[];
}

export type ChatMessage = Message;

export interface Conversation {
  id: string;
  name: string;
  type: 'direct' | 'group' | 'room' | 'dm'; // Combined types
  creator_id: string;
  is_private?: boolean;
  created_at: string;
  updated_at: string;
  participants?: User[] | string[];
  last_message?: Message;
  unread_count?: number;
}
/**
 * Track type - extends generated type from OpenAPI
 * Action 1.1.3.3: Base type from generated API types
 * Preserves UI-specific computed fields and aliases
 * 
 * Note: Generated type uses duration as number (seconds), but legacy code may use
 * formatted string. Both are supported for backward compatibility.
 */
export type Track = Omit<VezaBackendApiInternalModelsTrack, 'duration'> & {
  // Required fields (generated type has them as optional, but backend always provides them)
  id: string;
  creator_id: string;
  title: string;
  artist: string;
  file_path: string;
  file_size: number;
  format: string;
  is_public: boolean;
  play_count: number;
  like_count: number;
  created_at: string;
  updated_at: string;
  // Duration handling - support both number (seconds) and formatted string for backward compatibility
  duration: number | string; // seconds (number) or formatted "3:45" (string)
  durationSec?: number; // alias for duration when it's a number (backward compatibility)
  // Status uses TrackStatus enum for type safety
  status: TrackStatus | VezaBackendApiInternalModelsTrackStatus;
  // Stream status with specific union type
  stream_status: 'pending' | 'processing' | 'ready' | 'error';
  // Relations (optional)
  user?: User; // Backend relation User
  // Extended properties for UI (computed or aliases)
  coverUrl?: string; // Alias for cover_art_path (computed URL)
  plays?: number; // Alias for play_count
  likes?: number; // Alias for like_count
  isPremium?: boolean;
  lyrics?: { time: number; text: string }[];
  waveformData?: number[];
  tags?: string[];
  // File access
  filePath?: string; // Alias for file_path
};

export interface LibraryItem {
  id: string;
  type: 'track' | 'document' | 'playlist';
  title: string;
  description?: string;
  thumbnail?: string;
  item_id: string;
  added_at: string;
  is_favorite?: boolean;
}

export interface Playlist {
  id: string;
  title: string;
  description?: string;
  cover_url?: string;
  is_public: boolean;
  user_id: string;
  track_count: number;
  tracks?: Track[];
  created_at: string;
  updated_at: string;
  // UI compatibility
  coverUrl?: string;
  trackCount?: number;
  tags?: string[];
  follower_count?: number;
}

export interface PaginationData {
  total: number;
  page: number;
  limit: number;
  total_pages: number;
  has_next: boolean;
  has_prev: boolean;
}

export interface ApiResponse<T = any> {
  success: boolean;
  data: T;
  error?: ApiError;
  meta?: Record<string, any>;
}

export interface Notification {
  id: string;
  user_id: string;
  type:
    | 'info'
    | 'success'
    | 'warning'
    | 'error'
    | 'like'
    | 'follow'
    | 'mention'
    | 'sale'
    | 'security';
  title?: string;
  content: string;
  message?: string; // Compatibility
  read: boolean;
  action_url?: string;
  actionUrl?: string; // Compatibility
  timestamp: string;
  created_at?: string; // Compatibility
}

export interface Session {
  id: string;
  user_id: string;
  ip_address: string;
  user_agent: string;
  is_current: boolean;
  last_active: string;
  expires_at: string;
}

export interface AuditLog {
  id: string;
  user_id: string;
  action: string;
  resource: string;
  resource_id: string;
  ip_address: string;
  user_agent: string;
  timestamp: string;
  details?: Record<string, any>;
}

export interface AuditStats {
  action: string;
  count: number;
  resource?: string;
}

export interface SuspiciousActivity {
  id: string;
  user_id: string;
  reason: string;
  severity: 'low' | 'medium' | 'high' | 'critical';
  timestamp: string;
  details?: Record<string, any>;
}
