import { RoleEnum } from '@msfa-enums/role.enum';
import { mapStringToSsn } from '@msfa-utils/map-string-to-ssn.util';
import { EmployeeCompactResponse, EmployeeResponse } from './api/employee.response.model';
import { EmployeeTjanst, mapResponseToEmployeeTjanst } from './employee-tjanst.model';
import {
  EmployeeUtforandeVerksamhet,
  mapResponseToEmployeeUtforandeVerksamhet,
} from './employee-utforande-verksamhet.model';
import { PaginationMeta } from './pagination-meta.model';

export interface EmployeeCompact {
  id: string;
  fullName: string;
  tjanster: string[];
  allaUtforandeVerksamheter: boolean;
  utforandeVerksamheter: string[];
}
export interface Employee {
  id: string;
  firstName: string;
  lastName: string;
  fullName: string;
  email: string;
  ssn: string;
  roles: RoleEnum[];
  tjanster: EmployeeTjanst[];
  allaUtforandeVerksamheter: boolean;
  utforandeVerksamheter: EmployeeUtforandeVerksamhet[];
}

export interface EmployeesData {
  data: EmployeeCompact[];
  meta: PaginationMeta;
}

export function mapResponseToEmployeeCompact(data: EmployeeCompactResponse): EmployeeCompact {
  const { ciamUserId, name, tjanst, allaUtforandeVerksamheter, utforandeVerksamhet } = data;
  return {
    id: ciamUserId,
    fullName: name,
    tjanster: tjanst || [],
    allaUtforandeVerksamheter: allaUtforandeVerksamheter,
    utforandeVerksamheter: utforandeVerksamhet || [],
  };
}

export function mapResponseToEmployee(data: EmployeeResponse): Employee {
  const {
    ciamUserId,
    firstName,
    lastName,
    email,
    ssn,
    roles,
    tjanster,
    allaUtforandeVerksamheter,
    utforandeVerksamheter,
  } = data;
  return {
    id: ciamUserId,
    firstName,
    lastName,
    fullName: `${firstName} ${lastName}`,
    email,
    ssn: ssn ? mapStringToSsn(ssn) : null,
    roles: roles || [],
    tjanster: tjanster?.map(tjanst => mapResponseToEmployeeTjanst(tjanst)),
    allaUtforandeVerksamheter,
    utforandeVerksamheter: utforandeVerksamheter.map(utforandeVerksamhet =>
      mapResponseToEmployeeUtforandeVerksamhet(utforandeVerksamhet)
    ),
  };
}
