/**
 * Entity Types
 */

// Base entity interface that all entities extend
export interface BaseEntity {
  id: string;
  createdAt?: string;
  updatedAt?: string;
}

// User entity
export interface User extends BaseEntity {
  firstName?: string;
  lastName?: string;
  phoneNumber?: string;
  email: string;
  disabled?: boolean;
  avatar?: ImageFile[];
  app_role?: Role | null;
  custom_permissions?: PermissionEntity[];
  password?: string;
}

// Role entity
export interface Role extends BaseEntity {
  name: string;
  permissions?: PermissionEntity[];
}

// Permission entity (named PermissionEntity to avoid conflict with Permission enum)
export interface PermissionEntity extends BaseEntity {
  name: string;
}

// Project entity
export interface Project extends BaseEntity {
  name: string;
  slug?: string;
  description?: string;
  logo_url?: string;
  favicon_url?: string;
  og_image_url?: string;
}

// Asset entity
export interface Asset extends BaseEntity {
  project?: Project | string | null;
  name: string;
  asset_type: 'image' | 'video' | 'audio' | 'file';
  type?:
    | 'general'
    | 'icon'
    | 'background_image'
    | 'audio'
    | 'video'
    | 'transition'
    | 'logo'
    | 'favicon'
    | 'document';
  cdn_url?: string;
  storage_key?: string;
  mime_type?: string;
  size_mb?: number;
  width_px?: number;
  height_px?: number;
  duration_sec?: number;
  checksum?: string;
  is_public?: boolean;
  is_deleted?: boolean;
  deleted_at_time?: string | Date;
}

// Asset Variant entity
export interface AssetVariant extends BaseEntity {
  asset?: Asset | string | null;
  variant_type?: string;
  cdn_url?: string;
  width_px?: number;
  height_px?: number;
}

// Tour Page entity
export interface TourPage extends BaseEntity {
  project?: Project | string | null;
  title?: string;
  slug?: string;
  background_asset?: Asset | string | null;
  audio_asset?: Asset | string | null;
  is_start_page?: boolean;
  sort_order?: number;
}

// Page Element entity
export interface PageElement extends BaseEntity {
  page?: TourPage | string | null;
  element_type?: string;
  label?: string;
  icon_asset?: Asset | string | null;
  position_x?: number;
  position_y?: number;
  width?: number;
  height?: number;
  action_type?: string;
  action_target?: string;
  sort_order?: number;
}

// Page Link entity
export interface PageLink extends BaseEntity {
  source_page?: TourPage | string | null;
  target_page?: TourPage | string | null;
  element?: PageElement | string | null;
  transition?: Transition | string | null;
}

// Transition entity
export interface Transition extends BaseEntity {
  project?: Project | string | null;
  name?: string;
  transition_type?: 'fade' | 'slide' | 'zoom' | 'none';
  duration_ms?: number;
  video_asset?: Asset | string | null;
}

// Project Audio Track entity
export interface ProjectAudioTrack extends BaseEntity {
  project?: Project | string | null;
  environment?: 'dev' | 'stage' | 'production';
  source_key?: string;
  name?: string;
  slug?: string;
  url?: string;
  loop?: boolean;
  volume?: number | string;
  sort_order?: number | string;
  is_enabled?: boolean;
  // Legacy fields
  audio_asset?: Asset | string | null;
  track_type?: 'background' | 'effect';
  is_default?: boolean;
}

// Project Membership entity
export interface ProjectMembership extends BaseEntity {
  user?: User | string | null;
  project?: Project | string | null;
  access_level?: 'owner' | 'editor' | 'reviewer' | 'viewer';
  is_active?: boolean;
  invited_at?: string | Date | null;
  accepted_at?: string | Date | null;
  // Legacy field
  role?: 'owner' | 'editor' | 'viewer';
}

// Publish Event entity
export interface PublishEvent extends BaseEntity {
  project?: Project | string | null;
  user?: User | string | null;
  from_environment?: 'dev' | 'stage' | 'production';
  to_environment?: 'dev' | 'stage' | 'production';
  started_at?: string | Date | null;
  finished_at?: string | Date | null;
  status?:
    | 'queued'
    | 'running'
    | 'success'
    | 'failed'
    | 'pending'
    | 'processing'
    | 'published';
  error_message?: string;
  pages_copied?: number | string;
  transitions_copied?: number | string;
  audios_copied?: number | string;
  // Legacy fields
  published_by?: User | string | null;
  version?: string;
  title?: string;
  description?: string;
  published_url?: string;
}

// PWA Cache entity
export interface PwaCache extends BaseEntity {
  project?: Project | string | null;
  cache_key?: string;
  last_updated?: string;
  is_valid?: boolean;
}

// Presigned URL Request entity
export interface PresignedUrlRequest extends BaseEntity {
  project?: Project | string | null;
  user?: User | string | null;
  purpose?: 'upload' | 'download';
  asset_type?: 'image' | 'video' | 'audio' | 'file';
  requested_key?: string;
  mime_type?: string;
  requested_size_mb?: number | string;
  expires_at?: string | Date;
  status?: string;
  // Legacy/alternative field names
  storage_key?: string;
  operation?: 'upload' | 'download';
  used?: boolean;
}

// Access Log entity
export interface AccessLog extends BaseEntity {
  user?: User | string | null;
  project?: Project | string | null;
  environment?: 'admin' | 'stage' | 'production';
  path?: string;
  action?: string;
  ip_address?: string;
  user_agent?: string;
  accessed_at?: string | Date;
  metadata?: Record<string, unknown>;
}

// UI Element entity
export interface UIElement extends BaseEntity {
  page?: TourPage | string | null;
  element_type?: string;
  content?: string;
  position_x?: number;
  position_y?: number;
  width?: number;
  height?: number;
  styles?: Record<string, unknown>;
}

// Image file type for uploads
export interface ImageFile {
  id?: string;
  name?: string;
  publicUrl?: string;
  privateUrl?: string;
  sizeInBytes?: number;
}

// Entity name type union for type-safe entity references
export type EntityName =
  | 'users'
  | 'roles'
  | 'permissions'
  | 'projects'
  | 'project_memberships'
  | 'assets'
  | 'asset_variants'
  | 'tour_pages'
  | 'project_audio_tracks'
  | 'publish_events'
  | 'pwa_caches'
  | 'presigned_url_requests'
  | 'access_logs'
  | 'element_type_defaults';

// Entity type map for generic lookups
export interface EntityTypeMap {
  users: User;
  roles: Role;
  permissions: PermissionEntity;
  projects: Project;
  project_memberships: ProjectMembership;
  assets: Asset;
  asset_variants: AssetVariant;
  tour_pages: TourPage;
  project_audio_tracks: ProjectAudioTrack;
  publish_events: PublishEvent;
  pwa_caches: PwaCache;
  presigned_url_requests: PresignedUrlRequest;
  access_logs: AccessLog;
  element_type_defaults: UIElement;
}
