import type { Database } from '../../supabase/database.types'

// Row types from Supabase
export type ProjectRow = Database['public']['Tables']['prj_projects']['Row']
export type ProjectInsert = Database['public']['Tables']['prj_projects']['Insert']
export type ProjectUpdate = Database['public']['Tables']['prj_projects']['Update']

export type MilestoneRow = Database['public']['Tables']['prj_milestones']['Row']
export type MilestoneInsert = Database['public']['Tables']['prj_milestones']['Insert']
export type MilestoneUpdate = Database['public']['Tables']['prj_milestones']['Update']

export type TaskRow = Database['public']['Tables']['prj_tasks']['Row']
export type TaskInsert = Database['public']['Tables']['prj_tasks']['Insert']
export type TaskUpdate = Database['public']['Tables']['prj_tasks']['Update']

export type NoteRow = Database['public']['Tables']['prj_notes']['Row']
export type NoteInsert = Database['public']['Tables']['prj_notes']['Insert']

// Status enums
export type ProjectStatus = 'active' | 'on_track' | 'at_risk' | 'blocked' | 'completed' | 'paused'

export type ProjectKind = 'client_engagement' | 'internal' | 'research' | 'other'

export type MilestoneStatus = 'upcoming' | 'in_progress' | 'completed' | 'overdue' | 'blocked'

export type TaskStatus =
  | 'planned'
  | 'in_progress'
  | 'blocked'
  | 'completed'
  | 'cancelled'
  | 'submitted'
  | 'approved'
  | 'rejected'
  | 'revision_requested'

export type TaskType = 'documentation' | 'code' | 'report' | 'design' | 'other'

export type NoteType = 'call_note' | 'status_update' | 'issue' | 'blocker' | 'agent_learning'

// Filter types
export interface ProjectFilters {
  status?: ProjectStatus
  kind?: ProjectKind
  companyId?: string
  search?: string
}

export interface MilestoneFilters {
  status?: MilestoneStatus
  projectId?: string
}

export interface TaskFilters {
  status?: TaskStatus
  type?: TaskType
  milestoneId?: string
  projectId?: string
}

export interface NoteFilters {
  type?: NoteType
  projectId?: string
}

// Display types (for JOINed data)
export interface ProjectWithCounts extends ProjectRow {
  milestoneCount: number
  taskCount: number
  completedMilestones?: number
  completedTasks?: number
}

export interface ProjectDetail extends ProjectRow {
  milestones: MilestoneRow[]
  tasks: TaskRow[]
  company: { id: string; name: string; domain: string | null } | null
  deal: ProjectSourceDealRef | null
  client: ProjectClientRef | null
}

export interface ProjectSourceDealRef {
  id: string
  clientId?: string | null
  contactEmail: string
  stageKey: string | null
  stateKey: string | null
  sourceListId: string | null
  updatedAt: string
}

export interface ProjectClientRef {
  id: string
  name: string
  status: string
}

export interface MilestoneWithTasks extends MilestoneRow {
  tasks: TaskRow[]
}

// Checklist item stored in prj_milestones.checklist JSONB column
export interface ChecklistItem {
  id: string
  label: string
  completed: boolean
}

// ---------------------------------------------------------------------------
// CamelCase domain types (snake_case DB rows mapped via service.transformRow)
// ---------------------------------------------------------------------------

/**
 * CamelCase domain type for a project row.
 * Produced by ProjectsService.transformRow() from the snake_case ProjectRow.
 */
export interface Project {
  id: string
  organizationId: string
  name: string
  kind: ProjectKind
  status: ProjectStatus
  description: string | null
  dealId: string | null
  clientId: string | null
  clientCompanyId: string | null
  startDate: string | null
  targetEndDate: string | null
  actualEndDate: string | null
  contractValue: number | null
  metadata: Record<string, unknown> | null
  createdAt: string
  updatedAt: string
}

/**
 * CamelCase project with aggregated counts (for list views).
 */
export interface ProjectSummary extends Project {
  milestoneCount: number
  taskCount: number
  completedMilestones: number
  completedTasks: number
}
