"use client";

import { Column, ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import { DataTableColumnHeader } from "@/components/ui/table/data-table-column-header";
import {
  getActiveStatusLabel,
  getTrainingCategoryLabel,
} from "@/constants/thai-labels";
import type { Course } from "../../api/types";
import { CellAction } from "./cell-action";

export const columns: ColumnDef<Course>[] = [
  {
    id: "name",
    accessorKey: "name",
    header: ({ column }: { column: Column<Course, unknown> }) => (
      <DataTableColumnHeader column={column} title="หลักสูตร" />
    ),
    cell: ({ row }) => (
      <div className="flex min-w-[240px] flex-col">
        <span className="font-medium">{row.original.name}</span>
        <span className="text-muted-foreground text-xs">
          {row.original.course_code ?? "ไม่มีรหัส"}
          {row.original.organizer ? ` · ${row.original.organizer}` : ""}
        </span>
      </div>
    ),
  },
  {
    id: "category",
    accessorKey: "category",
    enableSorting: false,
    header: ({ column }: { column: Column<Course, unknown> }) => (
      <DataTableColumnHeader column={column} title="หมวดหมู่" />
    ),
    cell: ({ cell }) => (
      <Badge variant="outline">
        {getTrainingCategoryLabel(cell.getValue<Course["category"]>())}
      </Badge>
    ),
  },
  {
    accessorKey: "standard_hours",
    header: "ชั่วโมง",
  },
  {
    accessorKey: "training_cost",
    header: "ค่าใช้จ่าย",
    cell: ({ cell }) => {
      const value = cell.getValue<Course["training_cost"]>();
      return value === null ? "ไม่ระบุ" : value.toLocaleString("th-TH");
    },
  },
  // {
  //   accessorKey: "certificate_validity_months",
  //   header: "อายุใบรับรอง",
  //   cell: ({ cell }) => {
  //     const value = cell.getValue<Course["certificate_validity_months"]>();
  //     return value === null ? "ไม่หมดอายุ" : `${value} เดือน`;
  //   },
  // },
  {
    accessorKey: "is_required",
    header: "บังคับ",
    cell: ({ row }) => (
      <Badge variant={row.original.is_required ? "default" : "secondary"}>
        {row.original.is_required ? "บังคับ" : "ไม่บังคับ"}
      </Badge>
    ),
  },
  {
    accessorKey: "certificate_required",
    header: "ใบรับรอง",
    cell: ({ row }) => (
      <Badge
        variant={row.original.certificate_required ? "default" : "secondary"}
      >
        {row.original.certificate_required ? "ต้องใช้" : "ไม่ต้องใช้"}
      </Badge>
    ),
  },
  {
    id: "status",
    accessorFn: (row) => (row.is_active ? "active" : "inactive"),
    enableSorting: false,
    header: ({ column }: { column: Column<Course, unknown> }) => (
      <DataTableColumnHeader column={column} title="สถานะ" />
    ),
    cell: ({ row }) => (
      <Badge variant={row.original.is_active ? "default" : "secondary"}>
        {getActiveStatusLabel(row.original.is_active)}
      </Badge>
    ),
  },
  {
    id: "actions",
    cell: ({ row }) => <CellAction data={row.original} />,
  },
];
