import { alias } from "drizzle-orm/pg-core";
import { hubspotUsers } from "@/app/db/schema/crm/hubspot_user_table";
import { hubspotDealData } from "@/app/db/schema/crm/hubspot_deal_table";
import type { HubspotDeal } from "./types";
import type { InferSelectModel } from "drizzle-orm";

export const coordinatorUser = alias(hubspotUsers, "coordinator_user");
export const designerUser = alias(hubspotUsers, "designer_user");

export type DealRow = {
  deal: InferSelectModel<typeof hubspotDealData>;
  coordinator: string | null;
  designer: string | null;
};

export function mapDbRowToHubspotDeal(row: DealRow): HubspotDeal {
  const d = row.deal;
  return {
    id: d.id,
    dealId: d.dealId,
    dealname: d.dealname,
    dealstage: d.dealstage,
    companyId: d.companyId,
    projectCode: d.projectCode,
    landlordPropertyId: d.landlordPropertyId,
    uprn: d.uprn,
    outcome: d.outcome,
    outcomeNotes: d.outcomeNotes,
    majorConditionIssueDescription: d.majorConditionIssueDescription,
    majorConditionIssuePhotos: d.majorConditionIssuePhotos,
    majorConditionIssuePhotosS3: d.majorConditionIssuePhotosS3,
    coordinationStatus: d.coordinationStatus,
    designStatus: d.designStatus,
    bookingStatus: d.bookingStatus,
    pashubLink: d.pashubLink,
    sharepointLink: d.sharepointLink,
    dampMouldFlag: d.dampmouldGrowth,
    dampMouldAndRepairComments: d.dampMouldAndRepairComments,
    coordinationComments: d.coordination_comments,
    domnasurveyRequired: d.domnasurveyRequired,
    preSapScore: d.preSap,
    coordinator: row.coordinator,
    ioeV1Date: d.mtpCompletionDate,
    ioeV2Date: d.mtpReModelCompletionDate,
    ioeV3Date: d.ioeV3CompletionDate,
    proposedMeasures: d.proposedMeasures,
    approvedPackage: d.approvedPackage,
    designer: row.designer,
    designDate: d.designCompletionDate,
    actualMeasuresInstalled: d.actualMeasuresInstalled,
    installer: d.installer,
    installerHandover: d.installerHandover,
    lodgementStatus: d.lodgementStatus,
    measuresLodgementDate: d.measuresLodgementDate,
    fullLodgementDate: d.lodgementDate,
    confirmedSurveyDate: d.confirmedSurveyDate,
    confirmedSurveyTime: d.confirmedSurveyTime,
    surveyedDate: d.surveyedDate,
    designType: d.dealType,
    eiScore: d.eiScore,
    eiScorePotential: d.eiScorePotential,
    epcSapScore: d.epcSapScore,
    potentialPostSapScoreDropdown: d.potentialPostSapScoreDropdown,
    epcPrn: d.epcPrn,
    surveyType: d.surveyType,
    measuresForPibiOrdered: d.measuresForPibiOrdered,
    pibiOrderDate: d.pibiOrderDate,
    pibiCompletedDate: d.pibiCompletedDate,
    propertyHaltedDate: d.propertyHaltedDate,
    propertyHaltedReason: d.propertyHaltedReason,
    technicalApprovedMeasuresForInstall: d.technicalApprovedMeasuresForInstall,
    domnaSurveyType: d.domnaSurveyType,
    domnaSurveyDate: d.domnaSurveyDate,
    batch: d.batch,
    batchDescription: d.batchDescription,
    planningAuthority: d.planningAuthority,
    designatedArea: d.designatedArea,
    article4PdRights: d.article4PdRights,
    listedBuilding: d.listedBuilding,
    designConstraints: d.designConstraints,
    planningComments: d.planningComments,
    planningStatus: d.planningStatus,
    planningSuggestedApproach: d.planningSuggestedApproach,
    createdAt: d.createdAt,
    updatedAt: d.updatedAt,
  };
}
