import {
  bigserial,
  timestamp,
  pgTable,
  real,
  jsonb,
  bigint,
  pgEnum,
  integer,
  boolean,
} from "drizzle-orm/pg-core";
import { InferModel } from "drizzle-orm";
// import { C } from "drizzle-orm/db.d-cf0abe10";

export const solar = pgTable("solar", {
  id: bigserial("id", { mode: "bigint" }).primaryKey(),
  longitude: real("longitude").notNull(),
  latitude: real("latitude").notNull(),
  uprn: bigint("uprn", { mode: "bigint" }).notNull(),
  createdAt: timestamp("created_at", {
    precision: 6,
    withTimezone: true,
  })
    .defaultNow()
    .notNull(),
  updatedAt: timestamp("updated_at", {
    precision: 6,
    withTimezone: true,
  })
    .defaultNow()
    .notNull(),
  googleApiResponse: jsonb("google_api_response").notNull(),
});

// Create a table with the solar panel scenarios
export const scenarioType: [string, ...string[]] = ["unit", "building"];
export const scenarioTypeEnum = pgEnum("scenario_type", scenarioType);

export const solarScenario = pgTable("solar_scenario", {
  id: bigserial("id", { mode: "bigint" }).primaryKey(),
  solarId: bigint("solar_id", { mode: "bigint" })
    .notNull()
    .references(() => solar.id),
  scenrioType: scenarioTypeEnum("scenario_type").notNull(),
  numberPanels: integer("number_panels").notNull(),
  arrayKwhp: integer("array_kwhp").notNull(),
  lifetimeDcKwh: real("lifetime_dc_kwh").notNull(),
  yearlyDcKwh: real("yearly_dc_kwh").notNull(),
  lifetimeAcKwh: real("lifetime_ac_kwh"),
  yearlyAcKwh: real("yearly_ac_kwh"),
  cost: real("cost").notNull(),
  expectedPaybackYears: real("expected_payback_years"),
  panelledRoofArea: real("panelled_roof_area").notNull(),
  isDefault: boolean("is_default").notNull(),
});

// Define types for selecting and inserting data
export type Solar = InferModel<typeof solar, "select">;
export type SolarScenario = InferModel<typeof solarScenario, "select">;

interface Center {
  latitude: number;
  longitude: number;
}

interface BoundingBox {
  ne: Center;
  sw: Center;
}

interface ImageryDate {
  day: number;
  year: number;
  month: number;
}

interface SolarPanelConfig {
  panelsCount: number;
  yearlyEnergyDcKwh: number;
  roofSegmentSummaries: any[];
}

interface SolarPotential {
  solarPanels: any[];
  buildingStats: any;
  wholeRoofStats: any;
  panelWidthMeters: number;
  roofSegmentStats: any[];
  panelHeightMeters: number;
  solarPanelConfigs: SolarPanelConfig[];
  panelCapacityWatts: number;
  panelLifetimeYears: number;
  maxArrayAreaMeters2: number;
  maxArrayPanelsCount: number;
  maxSunshineHoursPerYear: number;
  carbonOffsetFactorKgPerMwh: number;
}

interface GoogleApiResponse {
  name: string;
  center: Center;
  regionCode: string;
  boundingBox: BoundingBox;
  imageryDate: ImageryDate;
  imageryQuality: string;
  solarPotential: SolarPotential;
  imageryProcessedDate: ImageryDate;
}

export interface SolarInterface {
  id: bigint;
  longitude: number;
  latitude: number;
  uprn: bigint;
  createdAt: Date;
  updatedAt: Date;
  googleApiResponse: GoogleApiResponse;
}

export interface RoofSegment {
  segmentIndex: number;
  areaMeters2: number;
  groundAreaMeters2: number;
  sunshineQuantiles: number[];
  center: {
    latitude: number;
    longitude: number;
  };
  pitchDegrees: number;
  azimuthDegrees: number;
  planeHeightAtCenterMeters: number;
}
