## MODIFIED Requirements

### Requirement: Position Resolution in Employee Import
For each row, the system SHALL resolve one position from "Vị trí", narrowed by the company scope and, when filled, by "Phòng ban". The company scope SHALL be:
- when `company_mode` is a specific company X: company X;
- when `company_mode` is `all` and "Công ty" is filled: every company whose name equals "Công ty";
- when `company_mode` is `all` and "Công ty" is blank: all companies.

Only positions that are available for assignment SHALL be candidates. A position is available exactly when its own status is active, which a department's status is written down onto.

After narrowing, when more than one position remains:
- if "Phòng ban" is blank and the remaining positions belong to departments with different names, the row SHALL be rejected with reason code `position_ambiguous` and a message asking to fill "Phòng ban";
- otherwise, if the company scope is all companies and the remaining positions belong to companies with different names, the row SHALL be rejected with reason code `position_ambiguous` and a message asking to fill "Công ty";
- otherwise, all remaining positions have the same position, department and company names, and the system SHALL take the one with the lowest id.

When no available position remains but at least one unavailable position would have matched, the row SHALL be reported with reason code `position_unavailable` rather than `position_not_found`.

The employee's department, company, default leader and the company used for annual-leave conversion SHALL be derived from the resolved position. The system SHALL NOT fall back to a default position.

#### Scenario: Same position name in two departments, disambiguated by department
- **WHEN** positions named "Developer" exist in department "Web" and in department "Game", and an admin imports two rows with "Vị trí" = "Developer" and "Phòng ban" = "Web" and "Game" respectively
- **THEN** the first employee is assigned the "Developer" position of department "Web"
- **AND** the second employee is assigned the "Developer" position of department "Game"

#### Scenario: Same position name in two companies, disambiguated by company
- **WHEN** positions named "Accountant" exist in company "TAT" and company "Suga", `company_mode` is `all`, and a row has "Vị trí" = "Accountant" and "Công ty" = "Suga"
- **THEN** the employee is assigned the "Accountant" position of company "Suga"

#### Scenario: Unique position name without department or company
- **WHEN** exactly one available position named "CEO (TAT)" exists within the company scope and a row has "Vị trí" = "CEO (TAT)" with blank "Phòng ban" and "Công ty"
- **THEN** the employee is assigned that position

#### Scenario: Ambiguous position, department missing
- **WHEN** available positions named "Developer" exist in departments "Web" and "Game" within the company scope and a row has "Vị trí" = "Developer" with blank "Phòng ban"
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_ambiguous` and a message asking to fill "Phòng ban"

#### Scenario: Same department name in two companies, company missing
- **WHEN** `company_mode` is `all`, available positions named "Sale" exist in a department named "Sales" of company "TAT" and of company "Suga", and a row has "Vị trí" = "Sale", "Phòng ban" = "Sales" and blank "Công ty"
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_ambiguous` and a message asking to fill "Công ty"

#### Scenario: Identical position, department and company names
- **WHEN** two departments named "Sales" exist in the same company, each with an available position named "Sale", and a row resolves to both of them
- **THEN** the employee is assigned the "Sale" position with the lowest id

#### Scenario: Companies with the same name
- **WHEN** `company_mode` is `all`, two companies are named "Suga", and a row has "Công ty" = "Suga"
- **THEN** available positions of both companies are candidates
- **AND** the row is resolved by the rules above instead of being rejected for the company name

#### Scenario: Position not found
- **WHEN** no position matches "Vị trí" within the company scope and the department filter
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_not_found`

#### Scenario: Deactivated position
- **WHEN** the only position matching "Vị trí" within the company scope and the department filter is inactive
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_unavailable`

#### Scenario: Position in a deactivated department
- **WHEN** the only position matching "Vị trí" belongs to a department that has been deactivated, which leaves the position inactive
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_unavailable`

#### Scenario: Inactive duplicate no longer makes a name ambiguous
- **WHEN** positions named "Developer" exist in department "Web" and department "Game", department "Game" is deactivated, and a row has "Vị trí" = "Developer" with blank "Phòng ban"
- **THEN** the employee is assigned the "Developer" position of department "Web"
- **AND** the row is not reported with reason code `position_ambiguous`

#### Scenario: Blank position
- **WHEN** a row has a blank "Vị trí"
- **THEN** the row is not imported
- **AND** the row is reported with reason code `position_required`

#### Scenario: Specific company mode scopes the lookup
- **WHEN** `company_mode` is company X, a row has blank "Công ty", and "Vị trí" matches exactly one available position in company X and other positions in other companies
- **THEN** the employee is assigned the position in company X

#### Scenario: Specific company mode with a different company in the file
- **WHEN** `company_mode` is company X and a row has "Công ty" filled with a name other than company X's name
- **THEN** the row is not imported
- **AND** the row is reported with reason code `company_mismatch`

#### Scenario: Unknown company name in all mode
- **WHEN** `company_mode` is `all` and "Công ty" does not match any company name
- **THEN** the row is not imported
- **AND** the row is reported with reason code `company_not_found`

### Requirement: Per-Row Validation in Employee Import
The system SHALL validate every non-empty row before creating the employee. A row that fails validation SHALL NOT be created, and all other valid rows SHALL still be created. A row SHALL fail with the given reason code when:
- "Họ", "Tên", "Email" or "SĐT" is blank: `required_field_missing`;
- the email already exists as an account username (including soft-deleted accounts), or appears in an earlier row of the same file: `email_duplicated`;
- the phone number already exists on a profile, or appears in an earlier row of the same file: `phone_duplicated`;
- "Mã NV" is filled and already exists, or appears in an earlier row of the same file: `code_duplicated`;
- a text value is longer than its column allows, including "Vị trí HĐ" longer than 191 characters: `value_too_long`;
- position or company resolution fails, using the reason codes of the Position Resolution requirement, including `position_unavailable` when the row's position exists but is not available for assignment.

A row SHALL collect all of its validation failures, not only the first one. An unexpected error while creating a row SHALL roll back that row only and SHALL be reported with reason code `unknown_error`.

#### Scenario: Duplicate employee code against the database
- **WHEN** a row's "Mã NV" equals an existing employee's code
- **THEN** the row is not imported and is reported with reason code `code_duplicated`
- **AND** the other valid rows in the file are imported

#### Scenario: Contract position too long
- **WHEN** a row's "Vị trí HĐ" is 192 characters long
- **THEN** the row is not imported and is reported with reason code `value_too_long` naming the "Vị trí HĐ" column

#### Scenario: Same email twice in one file
- **WHEN** two rows in the same file have the same email that does not exist in the database
- **THEN** the first row is imported
- **AND** the second row is not imported and is reported with reason code `email_duplicated`
- **AND** the first employee's position is not changed by the second row

#### Scenario: Same phone twice in one file
- **WHEN** two rows in the same file have the same phone number that does not exist in the database
- **THEN** the first row is imported
- **AND** the second row is reported with reason code `phone_duplicated`, not `unknown_error`

#### Scenario: Missing required field
- **WHEN** a non-empty row has a blank "Email"
- **THEN** the row is reported with reason code `required_field_missing`

#### Scenario: Multiple failures in one row
- **WHEN** a row has a duplicated "Mã NV" and a "Vị trí HĐ" longer than 191 characters
- **THEN** the reported entry for that row contains both `code_duplicated` and `value_too_long`

#### Scenario: Unavailable position alongside another failure
- **WHEN** a row has a duplicated "Mã NV" and a "Vị trí" whose only match is deactivated
- **THEN** the reported entry for that row contains both `code_duplicated` and `position_unavailable`
