import {
  bigserial,
  text,
  timestamp,
  pgTable,
  pgEnum,
  bigint,
} from "drizzle-orm/pg-core";
import { property } from "./property";

const inspection_archetypes: [string, ...string[]] = [
  "Bungalow",
  "Flat",
  "Maisonette",
  "House",
  "non-domestic",
];
export const inspectionArchetypeEnum = pgEnum(
  "inspection_archetype",
  inspection_archetypes
);

const inspection_archetypes_2: [string, ...string[]] = [
  "detached",
  "mid-terrace",
  "enclosed mid-terrace",
  "end-terrace",
  "enclosed end-terrace",
  "semi-detached",
];
export const inspectionArchetype2Enum = pgEnum(
  "inspection_archetype_2",
  inspection_archetypes_2
);

const inspections_wall_constructions: [string, ...string[]] = [
  "cavity",
  "solid",
  "system built",
  "timber framed",
  "steel framed",
  "re-walled cavity",
  "mansard pre-fab",
  "mansard ewi",
  "mansard re-walled",
];
export const inspectionsWallConstructionEnum = pgEnum(
  "inspections_wall_construction",
  inspections_wall_constructions
);

const inspections_wall_insulation: [string, ...string[]] = [
  "empty cavity",
  "filled at build",
  "partial",
  "retro drilled",
  "ewi",
  "iwi",
  "solid non-cavity",
  "system built",
  "timber framed",
  "steel framed",
];
export const inspectionsWallInsulationEnum = pgEnum(
  "inspections_wall_insulation",
  inspections_wall_insulation
);

const inspectionsInsulationMaterial: [string, ...string[]] = [
  "empty 50-90",
  "empty 100+",
  "empty 30-40",
  "empty less than 30",
  "loose fibre/wool",
  "eps/celo/king",
  "fibre batts - with cavity",
  "fibre batts - no cavity",
  "loose bead",
  "glued bead",
  "formaldehyde",
  "bubble wrap",
  "poly chunks",
];
export const inspectionsInsulationMaterialEnum = pgEnum(
  "inspections_insulation_material",
  inspectionsInsulationMaterial
);

const inspectionBorescoped: [string, ...string[]] = ["yes", "no", "refused"];
export const inspectionBorescopedEnum = pgEnum(
  "inspection_borescoped",
  inspectionBorescoped
);

const inspectionsRoofOrientations: [string, ...string[]] = [
  "north",
  "east",
  "south",
  "west",
  "north-east",
  "north-west",
  "south-east",
  "south-west",
  "n/s split",
  "e/w split",
  "ne/sw split",
  "nw/se split",
  "flat roof",
  "no roof",
  "roof too small",
  "already has solar pv",
];
export const inspectionsRoofOrientationEnum = pgEnum(
  "inspections_roof_orientation",
  inspectionsRoofOrientations
);

const inspectionsTileHung: [string, ...string[]] = [
  "yes",
  "no",
  "first floor flats are tile hung",
];
export const inspectionsTileHungEnum = pgEnum(
  "inspections_tile_hung",
  inspectionsTileHung
);

const renderedOptions: [string, ...string[]] = [
  "no render",
  "rendered with “insufficient” space between dpc and render",
  "rendered with “sufficient” space between dpc and render",
];
export const inspectionsRenderedEnum = pgEnum(
  "inspections_rendered",
  renderedOptions
);

const claddingOptions: [string, ...string[]] = [
  "none",
  "cladded with “sufficient space to fill the wall”",
  "cladded with “insufficient space to fill the wall”",
];

export const inspectionsCladdingEnum = pgEnum(
  "inspections_cladding",
  claddingOptions
);

const access_issuesOptions: [string, ...string[]] = [
  "see notes",
  "damp issues",
  "foliage on walls",
  "bushes against wall",
  "trees around/anove property",
  "high rise block flats/maisonettes",
  "conservatory",
  "lean-to",
  "garage",
  "extension",
  "decking",
  "shed against wall",
];

export const inspectionsAccessIssuesEnum = pgEnum(
  "inspections_access_issues",
  access_issuesOptions
);

export const inspections = pgTable("inspections", {
  id: bigserial("id", { mode: "bigint" }).primaryKey(),
  propertyId: bigint("property_id", { mode: "bigint" })
    .notNull()
    .references(() => property.id),
  // TODO Revise
  archetype: inspectionArchetypeEnum("archetype"),
  archetype2: inspectionArchetype2Enum("archetype_2"),
  wallConstruction: inspectionsWallConstructionEnum("wall_construction"),
  insulation: inspectionsWallInsulationEnum("insulation"),
  insulationMaterial: inspectionsInsulationMaterialEnum("insulation_material"),
  borescoped: inspectionBorescopedEnum("borescoped"),
  roofOrientation: inspectionsRoofOrientationEnum("roof_orientation"),
  tileHung: inspectionsTileHungEnum("tile_hung"),
  rendered: inspectionsRenderedEnum("rendered"),
  cladding: inspectionsCladdingEnum("cladding"),
  access_issues: inspectionsAccessIssuesEnum("access_issues"),
  notes: text("notes"),
  createdAt: timestamp("created_at").notNull(),
  surveyorName: text("surveyor_name"),
  uploadedAt: timestamp("uploaded_at").defaultNow().notNull(),
});
