You are a Senior Full Stack Engineer and Solution Architect.

Project:
Training Management System (TMS)

Current status:
Sprint 6.11 Identity / Employee Cleanup completed.

Current task:
Implement Sprint 6.12 Employee Ownership Final Migration and Employee Directory Data Source Validation.

Goal:
Finish migration from user-owned training data to employee-owned training data for employee directory, dashboard, reports, and remaining aggregation paths.

Important constraints:

- Do NOT break existing credential login.
- Do NOT remove legacy columns yet.
- Do NOT remove users.employeeCode fallback yet unless fully safe.
- Do NOT change business features.
- Keep Auth.js working.
- Keep Drizzle ORM.
- Keep Thai UI.
- Minimal-impact changes only.

Part 0: Employee Directory Data Source Validation

Update:

- src/features/employee-directory/server/employee-directory-data.ts
- src/app/dashboard/employee-directory/page.tsx
- src/app/dashboard/employee-directory/[id]/page.tsx
- related employee directory components

Requirements:

1. Employee Directory list must use employees as the primary source.

The page:

- รายชื่อพนักงานทั้งหมด

Must read from:

- employees

Not from:

- users

2. Employee Directory table must show:

- รหัสพนักงาน from employees.employeeCode
- ชื่อพนักงาน from employees.displayName or firstNameTh + lastNameTh
- ฝ่าย/แผนก from employees.departmentId / department relation
- ตำแหน่ง from employees.positionId / position relation

3. Employee Directory detail must use employees as the primary source.

Detail page must show:

ข้อมูลทั่วไปพนักงาน:

- employeeCode
- displayName
- email
- companyName / company
- department
- position
- status

4. Training summary must use:

- training_records.employee_id = employees.id

Calculate:

- approved hours
- pending hours
- rejected hours
- total submitted hours
- K/S/A approved hours

5. Target resolution must use:

Priority:

1. employee_training_targets.employee_id
2. training_policy default

Do not use userId target resolution as primary.

6. Search must use employees fields:

Search by:

- employees.employeeCode
- employees.displayName
- employees.email
- department name
- position name

7. Filters must use employees fields:

- department
- position
- status
- company if available

8. Permission:

HRD/Admin:

- can access employee directory

Employee:

- cannot access employee directory by menu or direct URL

9. Do not require employee to have user account.

Important:
An employee imported into employees but without a linked user account must still appear in Employee Directory.

Part 1: Overview Dashboard Migration

Update:

- src/features/overview/server/overview-data.ts

Requirements:

1. Employee dashboard must resolve current employee through:
   - user_employee_maps
   - fallback users.employeeId if still exists
   - fallback users.employeeCode only if needed

2. Employee dashboard must aggregate from:
   - employees
   - training_records.employee_id

3. HRD dashboard must use:
   - employees as employee source
   - training_records.employee_id as training ownership source

4. Pass/fail calculation must use:
   - employee_training_targets.employee_id if exists
   - fallback Training Policy

5. Avoid using users as the primary employee source.

Part 2: Reports Migration

Update:

- src/features/reports/server/report-data.ts
- src/app/api/reports/export/route.ts if needed

Requirements:

1. Employee Transcript:
   - Employee users see only their mapped employee record.
   - HRD/Admin can view organization employees.
   - Data source must be employees + training_records.employee_id.

2. Training Matrix:
   - Use employees + training_records.employee_id.
   - Keep existing matrix behavior.

3. Department Summary:
   - Count employees from employees table.
   - Aggregate training hours from training_records.employee_id.

4. Annual Summary:
   - Aggregate by employee-owned training records.

5. Export Excel/PDF must remain working.

Part 3: Employee Target Resolution

Update:

- src/features/training-policy/server/employee-training-target-data.ts

Requirements:

1. Resolve target by employeeId first.
2. Fallback to policy.
3. Avoid userId target resolution except legacy compatibility.

Part 4: Reduce Legacy Dependency

Search and review remaining usage of:

- users.employeeCode
- users.employeeId
- training_records.userId

Do not delete them yet.

Create a list of:

- still required
- can be migrated later
- safe to remove in future cleanup

Part 5: Regression

Validate:

- Employee login
- Employee dashboard
- HRD dashboard
- Employee Directory list
- Employee Directory detail
- Employee imported without user appears in Employee Directory
- Employee transcript
- Department summary
- Annual summary
- Training matrix
- Training record create
- HRD review
- Import employee
- Employee target import

Part 6: Output Review File

Create:
docs/sprint-6-12-employee-ownership-final-migration-review.md

Include:

1. Summary
2. Files Changed
3. Employee Directory Data Source Validation
4. Overview Migration
5. Reports Migration
6. Target Resolution Changes
7. Remaining Legacy Dependencies
8. Regression Checklist
9. Risks
10. Recommendation Before Sprint 7.2

Return:

- Complete code changes with file paths
- Review file
- Migration commands if required
