"use client";

import { Badge } from "@/components/ui/badge";
import { DataTableColumnHeader } from "@/components/ui/table/data-table-column-header";
import type { TrainingMatrix } from "../../api/types";
import { Column, ColumnDef } from "@tanstack/react-table";
import { Icons } from "@/components/icons";
import { CellAction } from "./cell-action";

export const columns: ColumnDef<TrainingMatrix>[] = [
  {
    id: "course_name",
    accessorKey: "course_name",
    header: ({ column }: { column: Column<TrainingMatrix, unknown> }) => (
      <DataTableColumnHeader column={column} title="หลักสูตร" />
    ),
    cell: ({ row }) => (
      <div className="flex min-w-[240px] flex-col">
        <span className="font-medium">{row.original.course_name}</span>
        <span className="text-muted-foreground text-xs">
          {row.original.course_category}
        </span>
      </div>
    ),
    meta: {
      label: "หลักสูตร",
      placeholder: "ค้นหาการอบรมตามตำแหน่ง...",
      variant: "text" as const,
      icon: Icons.text,
    },
    enableColumnFilter: true,
  },
  {
    id: "scope",
    accessorFn: (row) => row.department_name || row.position_name || "ส่วนกลาง",
    header: "ขอบเขต",
    cell: ({ row }) => (
      <div className="text-sm">
        {row.original.department_name ? (
          <div>แผนก: {row.original.department_name}</div>
        ) : null}
        {row.original.position_name ? (
          <div>ตำแหน่ง: {row.original.position_name}</div>
        ) : null}
        {!row.original.department_name && !row.original.position_name
          ? "ส่วนกลาง"
          : null}
      </div>
    ),
  },
  {
    accessorKey: "renewal_period_months",
    header: "อายุการทบทวน",
    cell: ({ row }) => {
      const months =
        row.original.renewal_period_months ??
        row.original.course_certificate_validity_months;
      return months === null ? "ไม่มีวันหมดอายุ" : `${months} เดือน`;
    },
  },
  {
    accessorKey: "is_required",
    header: "สถานะบังคับ",
    cell: ({ row }) => (
      <Badge variant={row.original.is_required ? "default" : "secondary"}>
        {row.original.is_required ? "บังคับ" : "ไม่บังคับ"}
      </Badge>
    ),
  },
  {
    id: "actions",
    cell: ({ row }) => <CellAction data={row.original} />,
  },
];
