import type { ExcelColumnDef } from '@/common/excel/excel-export.types';

import type { ExportLanguage } from './dto/export-working-group-issues.dto';
import type { WorkingGroupIssueResponse } from './working-group-issues.service';

const EXPORT_HEADERS: Record<
  ExportLanguage,
  Record<
    | 'no'
    | 'issue'
    | 'category'
    | 'description'
    | 'recommendation'
    | 'governmentDecision'
    | 'status'
    | 'primaryAgency'
    | 'issueReference'
    | 'solutionReference'
    | 'year'
    | 'attachment',
    string
  >
> = {
  en: {
    no: 'No',
    issue: 'Issue',
    category: 'Issue Category',
    description: 'Issue Description',
    recommendation: 'Recommendation',
    governmentDecision: 'Government Decision',
    status: 'Status',
    primaryAgency: 'Primary Agency',
    issueReference: 'Issue Reference',
    solutionReference: 'Solution Reference',
    year: 'Year',
    attachment: 'Attachment',
  },
  km: {
    no: 'ល.រ',
    issue: 'បញ្ហា',
    category: 'ប្រភេទបញ្ហា',
    description: 'ការពិពណ៌នាបញ្ហា',
    recommendation: 'សំណូមពរ',
    governmentDecision: 'សេចក្ដីសម្រេចរបស់រាជរដ្ឋាភិបាល',
    status: 'ស្ថានភាព',
    primaryAgency: 'អង្គភាពរដ្ឋាភិបាលចម្បង',
    issueReference: 'ឯកសារយោងបញ្ហា',
    solutionReference: 'ឯកសារយោងដំណោះស្រាយ',
    year: 'ឆ្នាំ',
    attachment: 'ឯកសារភ្ជាប់',
  },
};

const ISSUE_MATRIX_EXPORT_HEADERS: Record<
  ExportLanguage,
  Record<
    | 'no'
    | 'workingGroup'
    | 'issue'
    | 'category'
    | 'description'
    | 'recommendation'
    | 'status'
    | 'primaryAgency'
    | 'year'
    | 'attachment',
    string
  >
> = {
  en: {
    no: 'No',
    workingGroup: 'Working Group',
    issue: 'Issue',
    category: 'Issue Category',
    description: 'Issue Description',
    recommendation: 'Recommendation',
    status: 'Status',
    primaryAgency: 'Primary Agency',
    year: 'Year',
    attachment: 'Attachment',
  },
  km: {
    no: 'ល.រ',
    workingGroup: 'ក្រុមការងារ',
    issue: 'បញ្ហា',
    category: 'ប្រភេទបញ្ហា',
    description: 'ការពិពណ៌នាបញ្ហា',
    recommendation: 'សំណូមពរ',
    status: 'ស្ថានភាព',
    primaryAgency: 'អង្គភាពរដ្ឋាភិបាលចម្បង',
    year: 'ឆ្នាំ',
    attachment: 'ឯកសារភ្ជាប់',
  },
};

const EXPORT_COLUMN_KEYS = [
  'no',
  'issue',
  'category',
  'description',
  'recommendation',
  'governmentDecision',
  'status',
  'primaryAgency',
  'issueReference',
  'solutionReference',
  'year',
  'attachment',
] as const;

const ISSUE_MATRIX_EXPORT_COLUMN_KEYS = [
  'no',
  'workingGroup',
  'issue',
  'category',
  'description',
  'recommendation',
  'status',
  'primaryAgency',
  'year',
  'attachment',
] as const;

const EXPORT_COLUMN_WIDTHS: Record<
  (typeof EXPORT_COLUMN_KEYS)[number],
  number
> = {
  no: 8,
  issue: 30,
  category: 18,
  description: 40,
  recommendation: 40,
  governmentDecision: 40,
  status: 16,
  primaryAgency: 24,
  issueReference: 20,
  solutionReference: 22,
  year: 10,
  attachment: 12,
};

const ISSUE_MATRIX_EXPORT_COLUMN_WIDTHS: Record<
  (typeof ISSUE_MATRIX_EXPORT_COLUMN_KEYS)[number],
  number
> = {
  no: 8,
  workingGroup: 28,
  issue: 30,
  category: 18,
  description: 40,
  recommendation: 40,
  status: 16,
  primaryAgency: 24,
  year: 10,
  attachment: 12,
};

export function getWorkingGroupIssuesExportColumns(
  lang: ExportLanguage = 'en',
): ExcelColumnDef[] {
  const headers = EXPORT_HEADERS[lang];

  return EXPORT_COLUMN_KEYS.map((key) => ({
    key,
    header: headers[key],
    width: EXPORT_COLUMN_WIDTHS[key],
  }));
}

export function getIssueMatrixExportColumns(
  lang: ExportLanguage = 'en',
): ExcelColumnDef[] {
  const headers = ISSUE_MATRIX_EXPORT_HEADERS[lang];

  return ISSUE_MATRIX_EXPORT_COLUMN_KEYS.map((key) => ({
    key,
    header: headers[key],
    width: ISSUE_MATRIX_EXPORT_COLUMN_WIDTHS[key],
  }));
}

function getPrimaryAgencyName(issue: WorkingGroupIssueResponse): string {
  const primaryAgency = [...issue.governmentAgencies].sort(
    (firstAgency, secondAgency) =>
      firstAgency.agencyOrder - secondAgency.agencyOrder,
  )[0];

  return primaryAgency?.stakeholder?.name ?? '-';
}

function getIssueYear(createdAt: Date): string {
  return String(createdAt.getFullYear());
}

export function mapWorkingGroupIssueToExportRow(
  issue: WorkingGroupIssueResponse,
  index: number,
  lang: ExportLanguage = 'en',
): Record<string, string | number> {
  const yesLabel = lang === 'km' ? 'បាទ/ចាស' : 'Yes';
  const noLabel = lang === 'km' ? 'ទេ' : 'No';

  return {
    no: issue.id,
    issue: issue.title,
    category: issue.category?.name ?? '-',
    description: issue.description,
    recommendation: issue.recommendation,
    governmentDecision: '-',
    status: issue.issueStatus?.name ?? '-',
    primaryAgency: getPrimaryAgencyName(issue),
    issueReference: `Issue ${index + 1}`,
    solutionReference: '',
    year: getIssueYear(issue.createdAt),
    attachment: issue.attachment?.trim() ? yesLabel : noLabel,
  };
}

export function mapIssueMatrixToExportRow(
  issue: WorkingGroupIssueResponse,
  lang: ExportLanguage = 'en',
): Record<string, string | number> {
  const yesLabel = lang === 'km' ? 'បាទ/ចាស' : 'Yes';
  const noLabel = lang === 'km' ? 'ទេ' : 'No';

  return {
    no: issue.id,
    workingGroup: issue.stakeholder?.name ?? '-',
    issue: issue.title,
    category: issue.category?.name ?? '-',
    description: issue.description,
    recommendation: issue.recommendation,
    status: issue.issueStatus?.name ?? '-',
    primaryAgency: getPrimaryAgencyName(issue),
    year: getIssueYear(issue.createdAt),
    attachment: issue.attachment?.trim() ? yesLabel : noLabel,
  };
}

export function buildWorkingGroupIssuesExportFilename(): string {
  const today = new Date().toISOString().slice(0, 10);
  return `working-group-issues-${today}.xlsx`;
}

export function buildIssueMatrixExportFilename(): string {
  const today = new Date().toISOString().slice(0, 10);
  return `issue-matrix-${today}.xlsx`;
}
