import {
  createGetDB,
  type Generated,
  type Timestamp,
  type NamespaceDB,
  type NamespaceInsertable,
  type NamespaceSelectable,
  type NamespaceTable,
  type NamespaceTableName,
  type NamespaceTransaction,
  type NamespaceUpdateable,
} from "@tailor-platform/sdk/kysely";

export interface Namespace {
  "main-db": {
    BusinessPartner: {
      id: Generated<string>;
      name: string;
      type: "ORGANIZATION" | "INDIVIDUAL";
      companyId: string;
      preferredCurrencyId: string | null;
      status: "DRAFT" | "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    ContactPerson: {
      id: Generated<string>;
      partnerId: string;
      firstName: string;
      lastName: string;
      email: string | null;
      phone: string | null;
      jobTitle: string | null;
      department: string | null;
      isPrimary: boolean;
      status: "ACTIVE" | "INACTIVE";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PartnerAddress: {
      id: Generated<string>;
      partnerId: string;
      addressType: "BILLING" | "SHIPPING" | "REGISTERED" | "OTHER";
      line1: string;
      line2: string | null;
      city: string;
      state: string | null;
      postalCode: string;
      country: string;
      isDefault: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PartnerBankAccount: {
      id: Generated<string>;
      partnerId: string;
      bankName: string;
      accountHolderName: string;
      accountNumber: string | null;
      routingNumber: string | null;
      iban: string | null;
      swiftBic: string | null;
      currencyId: string;
      isDefault: boolean;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PartnerIdentification: {
      id: Generated<string>;
      partnerId: string;
      type: "TAX_ID" | "VAT" | "DUNS" | "COMPANY_REGISTRY" | "NATIONAL_ID" | "OTHER";
      value: string;
      issuingCountry: string | null;
      validFrom: Timestamp | null;
      validTo: Timestamp | null;
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }

    PartnerRole: {
      id: Generated<string>;
      partnerId: string;
      role: "CUSTOMER" | "SUPPLIER";
      createdAt: Generated<Timestamp>;
      updatedAt: Timestamp | null;
    }
  }
}

export const getDB = createGetDB<Namespace>();

export type DB<N extends keyof Namespace = keyof Namespace> = NamespaceDB<Namespace, N>;

export type Transaction<K extends keyof Namespace | DB = keyof Namespace> =
  NamespaceTransaction<Namespace, K>;

type TableName = NamespaceTableName<Namespace>;
export type Table<T extends TableName> = NamespaceTable<Namespace, T>;

export type Insertable<T extends TableName> = NamespaceInsertable<Namespace, T>;
export type Selectable<T extends TableName> = NamespaceSelectable<Namespace, T>;
export type Updateable<T extends TableName> = NamespaceUpdateable<Namespace, T>;
