//import { mysqlTable, int, varchar, boolean, decimal, datetime, index } from "drizzle-orm/mysql-core"
import { integer, varchar, boolean, decimal, timestamp, index, pgSchema, uuid, primaryKey } from "drizzle-orm/pg-core";

export const pokeSchema = pgSchema("pokemon");

export const tcgcards = pokeSchema.table('tcg_cards', {
	productId: integer().primaryKey(),
	productName: varchar({ length: 255 }).notNull(),
	productLineName: varchar({ length: 255 }).default("").notNull(),
	productLineUrlName: varchar({ length: 255 }).default("").notNull(),
	productStatusId: integer().default(0).notNull(),
	productTypeId: integer().default(0).notNull(),
	productUrlName: varchar({ length: 255 }).default("").notNull(),
	rarityName: varchar({ length: 100 }).default("").notNull(),
	sealed: boolean().default(false).notNull(),
	sellerListable: boolean().default(false).notNull(),
	setId: integer(),
	shippingCategoryId: integer(),
	duplicate: boolean().default(false).notNull(),
	foilOnly: boolean().default(false).notNull(),
	maxFulfillableQuantity: integer(),
	totalListings: integer(),
	score: decimal({ precision: 10, scale: 2, mode: 'number' }),
	lowestPrice: decimal({ precision: 10, scale: 2, mode: 'number' }),
	lowestPriceWithShipping: decimal({ precision: 10, scale: 2, mode: 'number' }),
	marketPrice: decimal({ precision: 10, scale: 2, mode: 'number' }),
	medianPrice: decimal({ precision: 10, scale: 2, mode: 'number' }),
	attack1: varchar({ length: 1024 }),
	attack2: varchar({ length: 1024 }),
	attack3: varchar({ length: 1024 }),
	attack4: varchar({ length: 1024 }),
	cardType: varchar({ length: 100 }),
	cardTypeB: varchar({ length: 100 }),
	energyType: varchar({ length: 100 }),
	flavorText: varchar({ length: 1000 }),
	hp: integer(),
	number: varchar({ length: 50 }).default("").notNull(),
	releaseDate: timestamp(),
	resistance: varchar({ length: 100 }),
	retreatCost: varchar({ length: 100 }),
	stage: varchar({ length: 100 }),
	weakness: varchar({ length: 100 }),
	artist: varchar({ length: 255 }),
});

export const cards = pokeSchema.table('cards', {
	cardId: integer().notNull().primaryKey().generatedAlwaysAsIdentity(),
	productId: integer().notNull(),
	variant: varchar({ length: 100 }).notNull(),
	productName: varchar({ length: 255 }),
	productLineName: varchar({ length: 255 }),
	productUrlName: varchar({ length: 255 }).default("").notNull(),
	rarityName: varchar({ length: 100 }),
	sealed: boolean().default(false).notNull(),
	setId: integer(),
	cardType: varchar({ length: 100 }),
	energyType: varchar({ length: 100 }),
	number: varchar({ length: 50 }),
	inumber: integer(),
	artist: varchar({ length: 255 }),
},
(table) => [
	index('idx_card_product_id').on(table.productId, table.variant),
]);

export const tcg_overrides = pokeSchema.table('tcg_overrides', {
	productId: integer().primaryKey(),
	productName: varchar({ length: 255 }),
	productLineName: varchar({ length: 255 }),
	productUrlName: varchar({ length: 255 }).default('').notNull(),
	rarityName: varchar({ length: 100 }),
	sealed: boolean().default(false).notNull(),
	setId: integer(),
	cardType: varchar({ length: 100 }),
	energyType: varchar({ length: 100 }),
	number: varchar({ length: 50 }),
	artist: varchar({ length: 255 }),
});

export const sets = pokeSchema.table('sets', {
	setId: integer().primaryKey(),
	setName: varchar({ length: 255 }).notNull(),
	setUrlName: varchar({ length: 255 }).notNull(),
	setCode: varchar({ length: 100 }).notNull(),
});

export const skus = pokeSchema.table('skus', {
	skuId: integer().primaryKey(),
	cardId: integer().default(0).notNull(),
	productId: integer().notNull(),
	condition: varchar({ length: 255 }).notNull(),
	language: varchar({ length: 100 }).notNull(),
	variant: varchar({ length: 100 }).notNull(),
	calculatedAt: timestamp(),
	highestPrice: decimal({ precision: 10, scale: 2 }),
	lowestPrice: decimal({ precision: 10, scale: 2 }),
	marketPrice: decimal({ precision: 10, scale: 2 }),
	priceCount: integer(),
},
(table) => [
	index('idx_product_id_condition').on(table.productId, table.variant, table.condition),
	index('idx_card_id_condition').on(table.cardId, table.condition),
]);

export const priceHistory = pokeSchema.table('price_history', {
	skuId: integer().notNull(),
	calculatedAt: timestamp().notNull(),
	marketPrice: decimal({ precision: 10, scale: 2 }),
},
(table) => [
	primaryKey({ name: 'pk_price_history', columns: [table.skuId, table.calculatedAt] })
]);

export const salesHistory = pokeSchema.table('sales_history',{
	skuId: integer().notNull(),
	orderDate: timestamp().notNull(),
	title: varchar({ length: 255 }),
	customListingId: varchar({ length: 255 }),
	language: varchar({ length: 100 }),
	listingType: varchar({ length: 100 }),
	purchasePrice: decimal({ precision: 10, scale: 2 }),
	quantity: integer(),
	shippingPrice: decimal({ precision: 10, scale: 2 })
},
(table) => [
	primaryKey({ name: 'pk_sales_history', columns: [table.skuId, table.orderDate] })
]);

export const inventory = pokeSchema.table('inventory',{
	inventoryId: uuid().primaryKey().notNull().defaultRandom(),
	userId: varchar({ length: 100 }).notNull(),
	catalogName: varchar({ length: 100 }),
	skuId: integer().notNull(),
	quantity: integer(),
	purchasePrice: decimal({ precision: 10, scale: 2 }),
	note: varchar({ length:255 }),
	createdAt: timestamp().notNull().defaultNow(),
},
(table) => [
	index('idx_userid_skuId').on(table.userId, table.skuId),
	index('idx_userid_catalogName').on(table.userId, table.catalogName)
]);

export const processingSkus = pokeSchema.table('processing_skus', {
	skuId: integer().primaryKey(),
});
