/**
 * Types TypeScript reflétant les réponses de l'API PHP (cf. src/Controllers/*).
 *
 * Convention : les ressources retournent l'objet directement ; les listes
 * retournent { data, meta? }. Les erreurs sont { error, message?, fields? }.
 */

export type Dimension = 'social' | 'ecologique' | 'transversal'
export type ProjectStatus = 'active' | 'completed' | 'archived'
export type MemberKind = 'person' | 'collective' | 'org'
export type MemberRole = 'admin' | 'contributor' | 'observer'
export type ResourceKind = 'link' | 'document' | 'dataset' | 'video' | 'other'

export interface Category {
  id: number
  slug: string
  name: string
  description: string | null
  color_hex: string
  icon: string | null
  dimension: Dimension
  sort_order: number
  created_at: string
  updated_at: string
  /** Présent uniquement sur GET /api/categories/:slug */
  projects_count?: number
}

export interface Member {
  id: number
  slug: string
  display_name: string
  initials: string | null
  kind: MemberKind
  bio: string | null
  avatar_url: string | null
  website_url: string | null
  contact_email: string | null
  created_at: string
  updated_at: string
  /** Présent uniquement sur GET /api/members/:slug */
  projects?: Array<MemberProjectRef>
}

export interface MemberProjectRef {
  id: number
  slug: string
  title: string
  status: ProjectStatus
  progress_percent: number
  role: MemberRole
  joined_at: string
  category: { slug: string; name: string; color_hex: string }
}

export interface ProjectListRow {
  id: number
  slug: string
  title: string
  status: ProjectStatus
  territory: string
  short_description: string | null
  cover_image_url: string | null
  external_url: string | null
  progress_percent: number
  started_at: string | null
  completed_at: string | null
  created_at: string
  updated_at: string
  category: {
    id: number
    slug: string
    name: string
    color_hex: string
    dimension: Dimension
  }
}

export interface ProjectDetail {
  id: number
  slug: string
  title: string
  status: ProjectStatus
  territory: string
  short_description: string | null
  context_description: string | null
  objectives: string | null
  cover_image_url: string | null
  external_url: string | null
  progress_percent: number
  started_at: string | null
  completed_at: string | null
  created_at: string
  updated_at: string
  category: {
    id: number
    slug: string
    name: string
    color_hex: string
    icon: string | null
    dimension: Dimension
  } | null
  members: Array<ProjectMemberEntry>
  roadmap: Array<RoadmapStep>
  counts: {
    discussions: number
    resources: number
    members: number
  }
}

export interface ProjectMemberEntry {
  id: number
  slug: string
  display_name: string
  initials: string | null
  kind: MemberKind
  avatar_url: string | null
  role: MemberRole
  joined_at: string
}

export interface RoadmapStep {
  id: number
  project_id: number
  title: string
  description: string | null
  step_date: string | null
  is_done: boolean
  sort_order: number
  created_at: string
  updated_at: string
}

export interface Discussion {
  id: number
  project_id: number
  parent_id: number | null
  author_name: string
  author_member_id: number | null
  body: string
  created_at: string
  updated_at: string
  /** Présent uniquement sur les messages racines (GET .../discussions). */
  replies?: Discussion[]
}

export interface Resource {
  id: number
  project_id: number
  title: string
  description: string | null
  url: string
  kind: ResourceKind
  sort_order: number
  created_at: string
  updated_at: string
}

export interface ProjectImage {
  id: number
  project_id: number
  url: string
  caption: string | null
  sort_order: number
  /** Présent si upload disque, null si URL externe. */
  storage_path: string | null
  mime_type: string | null
  size_bytes: number | null
  width: number | null
  height: number | null
  created_at: string
  updated_at: string
}

export type ActivityVerb =
  | 'joined'
  | 'left'
  | 'created_project'
  | 'updated_project'
  | 'completed_step'
  | 'added_step'
  | 'posted_discussion'
  | 'added_resource'
  | 'completed_project'

export interface ActivityEvent {
  id: number
  project_id: number | null
  member_id: number | null
  actor_label: string
  verb: ActivityVerb
  summary: string
  occurred_at: string
}

export interface Stats {
  projects_active: number
  projects_completed: number
  projects_archived: number
  projects_total: number
  participants_total: number
  by_category: Array<{ slug: string; name: string; color_hex: string; count: number }>
  by_dimension: Record<Dimension, number>
}

/* ------- Réponses enveloppées ------- */

export interface Paginated<T> {
  data: T[]
  meta: { page: number; per_page: number; total: number; total_pages: number }
}

export interface Wrapped<T> {
  data: T[]
}

/* ------- Erreurs ------- */

export type ErrorCode =
  | 'bad_request'
  | 'unauthorized'
  | 'not_found'
  | 'method_not_allowed'
  | 'conflict'
  | 'validation_failed'
  | 'internal_server_error'

export interface ApiErrorPayload {
  error: ErrorCode
  message?: string
  fields?: Record<string, string>
  allowed?: string[]
}
