# AI Report

## Summary

Improved the employee import flow so repeated rows with the same `employeeCode` are treated as one employee while still creating a training history record for each course row. The import summary and template were updated to reflect employee creation/update, linked users, imported training records, and K/S/A target handling.

## Files Changed

- `src/app/api/import-employees/route.ts`
- `src/app/api/import-employees/template/route.ts`
- `src/constants/thai-labels.ts`
- `src/features/import-employees/api/types.ts`
- `src/features/import-employees/components/employee-import-page.tsx`
- `docs/ai/report.md`

## Root Cause / Reason For Change

- The original import flow only handled employee master data and K/S/A targets.
- Duplicate `employeeCode` rows in a single file were rejected instead of being merged into one employee with many training rows.
- The result summary contract was also inconsistent between backend and frontend.

## Changes Made

- Split import headers into employee headers, training headers, and target headers.
- Expanded the downloadable template to include course name, category, training hours, training type, and training date.
- Changed import parsing to group rows by `employeeCode`.
- Kept employee creation/updating at one record per `employeeCode`.
- Added validation for per-row training history data:
  - course name
  - category `K/S/A`
  - training hours
  - optional training type with normalization
  - optional training date with fallback to import year
- Created one approved `training_records` row for each valid training row.
- Preserved K/S/A employee target import and fallback behavior.
- Fixed import summary fields so frontend and backend now use the same names.
- Updated the import result UI to show:
  - created employees
  - updated employees
  - linked users
  - imported training records
  - imported targets

## Testing Checklist

- [x] `employeeCode` repeated in one file creates one employee
- [x] repeated employee rows still create multiple training records
- [x] existing employee in database is updated instead of duplicated
- [x] import summary returns consistent field names
- [x] downloadable template includes training history columns
- [x] `npx tsc --noEmit` passes
- [x] `npm run lint` passes with existing repo warnings only
- [ ] manually upload a file with one employee and many course rows
- [ ] manually upload a file for an existing employee and confirm no duplicate employee is created
- [ ] confirm overview/report/training history totals reflect imported approved records

## Risks

- Imported training records are marked as approved immediately so they affect dashboards and reports right away.
- If the same file is imported again, training history rows may be duplicated because this change intentionally deduplicates employees, not course rows.
- The audit log summary text still falls back to the generic import message shape because only the import contract was changed in this task.

## Rollback Plan

1. Revert `src/app/api/import-employees/route.ts`.
2. Revert `src/app/api/import-employees/template/route.ts`.
3. Revert `src/constants/thai-labels.ts`.
4. Revert `src/features/import-employees/api/types.ts`.
5. Revert `src/features/import-employees/components/employee-import-page.tsx`.
6. Restore or remove `docs/ai/report.md` as needed.

---

## Thai UI Fix Report

### Summary

Fixed corrupted Thai text on `/dashboard/import-employees` after the previous import work. The issue was caused by Thai literals being saved in a broken encoding state in the import page, shared Thai label constants, and related import API/template files.

### Files Changed

- `src/constants/thai-labels.ts`
- `src/features/import-employees/components/employee-import-page.tsx`
- `src/app/api/import-employees/template/route.ts`
- `src/app/api/import-employees/route.ts`
- `src/components/themes/font.config.ts`
- `src/styles/themes/training-system.css`
- `docs/ai/report.md`

### Root Cause

- Some Thai strings were rewritten from garbled terminal output during the previous task.
- The `/dashboard/import-employees` page and related API messages therefore showed mojibake instead of readable Thai.
- The main `training-system` theme also did not explicitly prioritize a Thai-capable web font.

### Changes Made

- Restored all corrupted Thai labels in `src/constants/thai-labels.ts`.
- Rewrote the import employee UI copy in `src/features/import-employees/components/employee-import-page.tsx` with readable Thai text.
- Restored Thai sample data and error text in `src/app/api/import-employees/template/route.ts`.
- Restored Thai validation and response messages in `src/app/api/import-employees/route.ts` without changing the import logic.
- Added `Noto Sans Thai` in `src/components/themes/font.config.ts`.
- Updated `src/styles/themes/training-system.css` to prioritize the Thai font in the main dashboard theme.

### Testing Checklist

- [x] `rg` no longer finds mojibake text in import-employees related files
- [ ] open `/dashboard/import-employees` and verify all Thai text reads normally
- [ ] try upload with invalid file and confirm Thai error messages display correctly
- [ ] download template and verify Thai headers/sample values are readable in Excel

### Notes

- This task intentionally did not change employee import business logic.
- The fix is focused on Thai text rendering and Thai-readable feedback only.

---

## Extract Total Hours Report

### Summary

Removed `ชั่วโมงรวม`, `ชั่วโมง K`, `ชั่วโมง S`, and `ชั่วโมง A` from the employee import template and import flow. The import now accepts only employee data plus training history rows, while hour summaries remain derived from real `Training Record` data.

### Files Changed

- `src/constants/thai-labels.ts`
- `src/app/api/import-employees/template/route.ts`
- `src/app/api/import-employees/route.ts`
- `src/features/import-employees/components/employee-import-page.tsx`
- `src/features/import-employees/api/types.ts`
- `docs/ai/report.md`

### Columns Removed From Import Template

- `ชั่วโมงรวม`
- `ชั่วโมง K`
- `ชั่วโมง S`
- `ชั่วโมง A`

### Logic Changes

- Removed target-hour columns from `thaiImportTemplateHeaders`.
- Updated the downloadable Excel template so it no longer includes K/S/A summary columns.
- Removed Excel parsing and validation for total/K/S/A hours from the import route.
- Removed import-time writes for employee target-hour values from the import route.
- Kept employee creation/update and `Training Record` creation as before.
- Updated the import page copy so it explains that hour summaries are calculated automatically from `Training Record`.
- Removed import summary cards related to imported K/S/A targets and training-policy fallback.

### Testing

- `npx tsc --noEmit`
- Verify template headers no longer contain total/K/S/A columns
- Verify import still accepts repeated employee rows with multiple course rows
- Verify successful import still creates employee + training records

### Result

- Import template no longer exposes total/K/S/A hour columns.
- Import no longer treats Excel hour-summary fields as source of truth.
- Hour summaries remain based on real training records only.
