// Shared client-side types mirroring the API JSON shapes.

export type Role = "SUPER_ADMIN" | "ADMIN" | "MANAGER" | "STAFF";

export interface UserDTO {
  id: string;
  name: string;
  email: string;
  role: Role;
  position: string | null;
  departmentId?: string | null;
  department?: { id: string; name: string } | null;
  active: boolean;
  createdAt?: string;
}

export interface CompanyDTO {
  id: string;
  name: string;
  active: boolean;
}

export interface DepartmentDTO {
  id: string;
  name: string;
  active: boolean;
  spansAll: boolean;
}

export interface PlatformDTO {
  id: string;
  name: string;
  active: boolean;
}

export interface ChannelDTO {
  id: string;
  name: string;
  active: boolean;
}

export interface KpiDTO {
  id: string;
  name: string;
  description: string | null;
  targetValue: number;
  currentValue: number;
  unit: string;
  period: "WEEKLY" | "MONTHLY" | "QUARTERLY";
  type: "DEPARTMENT" | "PERSONAL";
  companyId: string | null;
  company: { id: string; name: string } | null;
  departmentId: string | null;
  department: { id: string; name: string } | null;
  assignedUserId: string | null;
  assignedUser: { id: string; name: string; position: string | null } | null;
  assignedPosition: string | null;
  category: string;
  // Legacy single channel (may be present on older records).
  channel: string | null;
  // Editable, multi-select marketing channels.
  channels: { id: string; name: string }[];
  // Per-month values for the requested month (0 or 1 entry when ?year&month
  // are passed; all entries otherwise).
  monthlyValues?: KpiMonthlyValueDTO[];
  weightage: number;
}

export interface KpiMonthlyValueDTO {
  id: string;
  year: number;
  month: number;
  targetValue: number | null;
  actualValue: number;
  note: string | null;
}

export type PostStatus =
  | "DRAFT"
  | "PENDING_APPROVAL"
  | "APPROVED"
  | "REJECTED"
  | "POSTED";

export interface PostDTO {
  id: string;
  title: string;
  content: string;
  mediaUrl: string | null;
  mediaNotes: string | null;
  status: PostStatus;
  scheduledDate: string | null;
  postedDate: string | null;
  reviewNote: string | null;
  authorId: string;
  author: { id: string; name: string; position: string | null };
  reviewer: { id: string; name: string } | null;
  platforms: { id: string; name: string }[];
  updatedAt: string;
}

export interface ContributionDTO {
  id: string;
  taskId: string;
  kpiIndicatorId: string;
  impactDescription: string | null;
  metricDelta: number;
  kpiIndicator: KpiDTO;
}

export interface TaskDTO {
  id: string;
  title: string;
  description: string | null;
  status: string;
  priority: string;
  channel: string | null;
  dueDate: string | null;
  weekNumber: number;
  weekYear: number;
  userId: string;
  createdById: string;
  updatedAt: string;
  user: { id: string; name: string; position: string | null };
  contributions: ContributionDTO[];
}

export interface ReportDTO {
  teamScore: number;
  kpiProgress: {
    id: string;
    name: string;
    unit: string;
    period: string;
    channel: string | null;
    targetValue: number;
    currentValue: number;
    weightage: number;
    pct: number;
  }[];
  matrix: {
    id: string;
    name: string;
    position: string | null;
    tasksTotal: number;
    tasksCompleted: number;
    totalDelta: number;
  }[];
  statusBreakdown: { status: string; count: number }[];
  channelBreakdown: { channel: string; count: number }[];
  totals: { tasks: number; completed: number; staff: number };
}

export interface MonthlyReportKpi {
  id: string;
  name: string;
  unit: string;
  category: string;
  weightage: number;
  channels: string[];
  target: number;
  actual: number;
  prevActual: number | null;
  delta: number | null;
  note: string | null;
  pct: number;
}

export interface MonthlyReportDTO {
  year: number;
  month: number;
  departmentScore: number;
  personalScore: number;
  totals: { departmentKpis: number; personalKpis: number; staff: number };
  department: {
    companyId: string | null;
    companyName: string;
    score: number;
    kpis: MonthlyReportKpi[];
  }[];
  personal: {
    userId: string | null;
    name: string;
    position: string | null;
    score: number;
    kpis: MonthlyReportKpi[];
  }[];
}

// ── Executive dashboard ──
export interface ExecMetrics {
  budget: number;
  adSpend: number;
  revenue: number;
  leads: number;
  trials: number;
  enrollments: number;
  roas: number | null;
  roi: number | null;
  cac: number | null;
  cpl: number | null;
  leadToTrial: number | null;
  trialToEnroll: number | null;
  leadToEnroll: number | null;
  budgetVariancePct: number | null;
  overBudget: boolean;
  budgetUsedPct: number | null;
}

export interface ExecCompanyRow extends ExecMetrics {
  id: string;
  name: string;
}

export interface ExecutiveDTO {
  year: number;
  month: number;
  overall: ExecMetrics;
  deltas: {
    revenue: number | null;
    adSpend: number | null;
    enrollments: number | null;
    leads: number | null;
    roas: number | null;
    roi: number | null;
    cac: number | null;
    cpl: number | null;
    leadToEnroll: number | null;
  };
  funnel: {
    leads: number;
    trials: number;
    enrollments: number;
    leadToTrial: number | null;
    trialToEnroll: number | null;
    leadToEnroll: number | null;
  };
  companies: ExecCompanyRow[];
  categories: { category: string; kpiCount: number; attainment: number }[];
}

// ── Appraisals ──
export interface AppraisalCompetencyDTO {
  id: string;
  name: string;
  score: number | null;
  note: string | null;
  order: number;
}

export interface AppraisalListItem {
  id: string;
  year: number;
  half: "H1" | "H2";
  status: "DRAFT" | "SELF_REVIEW" | "MANAGER_REVIEW" | "FINALIZED" | "ACKNOWLEDGED";
  overallBand: string | null;
  user: { id: string; name: string; position: string | null };
  reviewer: { id: string; name: string } | null;
  updatedAt: string;
}

export interface AppraisalSnapshot {
  kpiAttainment: number | null;
  kpiCount: number;
  tasksTotal: number;
  tasksCompleted: number;
  postsPublished: number;
  competencyAvg: number | null;
}

export interface AppraisalDTO extends AppraisalListItem {
  strengths: string | null;
  improvements: string | null;
  managerComments: string | null;
  remarks: string | null;
  goalsReview: string | null;
  goalsNext: string | null;
  selfComments: string | null;
  selfRating: number | null;
  acknowledgedAt: string | null;
  user: { id: string; name: string; position: string | null; email: string };
  competencies: AppraisalCompetencyDTO[];
  snapshot: AppraisalSnapshot;
}

export interface MeDTO {
  user: UserDTO;
  stats: {
    tasksTotal: number;
    tasksCompleted: number;
    inProgress: number;
    totalDelta: number;
  };
  personalKpis: {
    id: string;
    name: string;
    description: string | null;
    unit: string;
    period: string;
    targetValue: number;
    currentValue: number;
    pct: number;
  }[];
}
