import { mapMyinfoSnapshot } from "./application-assessment";
import { mapMyinfoProfile, type MyinfoProfile } from "./myinfo-profile";

const isoDate = (date: Date) => date.toISOString().slice(0, 10);
const monthStart = (now: Date, monthsAgo: number, day = 1) =>
  new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() - monthsAgo, day));
const employer = "NORTHPOINT LOGISTICS PTE LTD";
function demoPerson(now: Date) {
  const metadata = {
      source: "1",
      lastupdated: isoDate(new Date(now.getTime() - 14 * 24 * 3600_000)),
    },
    verified = { ...metadata, source: "4" };
  const history = [3, 2, 1].map((paidMonthsAgo) => ({
    date: { value: isoDate(monthStart(now, paidMonthsAgo, 12)) },
    month: { value: isoDate(monthStart(now, paidMonthsAgo + 1)).slice(0, 7) },
    employer: { value: employer },
    amount: { value: 1480 },
  }));
  const latest = now.getUTCFullYear() - (now.getUTCMonth() < 7 ? 1 : 0);
  const noas = [
    [latest, 54000],
    [latest - 1, 51600],
  ].map(([year, income]) => ({
    yearofassessment: { value: String(year) },
    category: { value: "ORIGINAL" },
    taxclearance: { value: "N" },
    amount: { value: income },
    employment: { value: income },
    trade: { value: 0 },
    rent: { value: 0 },
    interest: { value: 0 },
  }));
  const address = {
    type: "SG",
    block: { value: "123" },
    street: { value: "ANG MO KIO AVENUE 6" },
    building: { value: "" },
    floor: { value: "08" },
    unit: { value: "112" },
    postal: { value: "560123" },
    country: { code: "SG", desc: "SINGAPORE" },
  };
  return {
    name: { ...metadata, value: "TAN WEI LING" },
    email: { ...verified, value: "weiling.tan@maxcredit-preview.sg" },
    mobileno: {
      ...verified,
      prefix: { value: "+" },
      areacode: { value: "65" },
      nbr: { value: "91234567" },
    },
    uinfin: { ...metadata, value: "S9812381D" },
    aliasname: { ...metadata, value: "" },
    dob: { ...metadata, value: "1998-01-15" },
    nationality: { ...metadata, code: "SG", desc: "SINGAPORE CITIZEN" },
    birthcountry: { ...metadata, code: "SG", desc: "SINGAPORE" },
    residentialstatus: { ...metadata, code: "C", desc: "CITIZEN" },
    sex: { ...metadata, code: "F", desc: "FEMALE" },
    marital: { ...metadata, code: "2", desc: "MARRIED" },
    regadd: { ...metadata, ...address },
    hdbtype: { ...metadata, code: "114", desc: "4-ROOM FLAT (HDB)" },
    housingtype: { ...metadata, code: "", desc: "" },
    hdbownership: [
      {
        ...metadata,
        noofowners: { value: 2 },
        hdbtype: { code: "114", desc: "4-ROOM FLAT (HDB)" },
        address,
      },
    ],
    cpfcontributions: { ...metadata, history },
    noahistory: { ...metadata, noas },
    employment: { ...metadata, value: employer },
    occupation: { ...metadata, value: "Operations Executive" },
  };
}
export const demoPersona = { name: "TAN WEI LING", maskedId: "S****381D" };
export function demoMyinfoProfile(now = new Date()): MyinfoProfile {
  const person = demoPerson(now);
  const snapshot = mapMyinfoSnapshot(person, "staging", now.toISOString());
  return {
    ...mapMyinfoProfile(person),
    evidence: {
      snapshot,
      receipt: "preview-demo-receipt",
      expiresAt: new Date(now.getTime() + 7 * 24 * 3600_000).toISOString(),
    },
  };
}
