import {
  db,
  type TailorAnyDBField,
  unsafeAllowAllGqlPermission,
  unsafeAllowAllTypePermission,
} from "@tailor-platform/sdk";
import { company } from "./company";

export interface CreateSiteTypeParams<F extends Record<string, TailorAnyDBField>> {
  fields?: F;
}

export function createSiteType<const F extends Record<string, TailorAnyDBField>>(
  params: CreateSiteTypeParams<F>,
) {
  return db
    .type("Site", {
      name: db.string().description("Site display name"),
      type: db
        .string()
        .description("Site type: OFFICE, WAREHOUSE, STORE, FACTORY, DISTRIBUTION_CENTER"),
      companyId: db
        .uuid()
        .relation({
          type: "n-1",
          toward: { type: company },
          backward: "sites",
        })
        .description("Foreign key to Company"),
      street: db.string().description("Site address street"),
      city: db.string().description("Site address city"),
      state: db.string({ optional: true }).description("Site address state/province"),
      postalCode: db.string().description("Site address postal code"),
      country: db.string().description("Site address country code"),
      timezone: db.string().description("IANA timezone identifier"),
      status: db.string().description("Site lifecycle status: ACTIVE, INACTIVE"),
      ...((params.fields ?? {}) as F),
      ...db.fields.timestamps(),
    })
    .permission(unsafeAllowAllTypePermission)
    .gqlPermission(unsafeAllowAllGqlPermission);
}

export const site = createSiteType({});
