import {
    pgTable,
    text,
    timestamp,
    jsonb,
} from 'drizzle-orm/pg-core';
import { relations, type InferSelectModel, type InferInsertModel } from 'drizzle-orm';

export type GameSettings = InferSelectModel<typeof gameSettings>;
export type GameSettingsInsert = InferInsertModel<typeof gameSettings>;

export interface LevelingConfig {
    base: number;
    exponent: number;
    chat: {
        cooldownMs: number;
        minXp: number;
        maxXp: number;
    };
}

export interface EconomyConfig {
    daily: {
        amount: string;
        streakBonus: string;
        weeklyBonus: string;
        cooldownMs: number;
    };
    transfers: {
        allowSelfTransfer: boolean;
        minAmount: string;
    };
    exam: {
        multMin: number;
        multMax: number;
    };
}

export interface InventoryConfig {
    maxStackSize: string;
    maxSlots: number;
}

export interface LootdropConfig {
    activityWindowMs: number;
    minMessages: number;
    spawnChance: number;
    cooldownMs: number;
    reward: {
        min: number;
        max: number;
        currency: string;
    };
}

export interface TriviaConfig {
    entryFee: string;
    rewardMultiplier: number;
    timeoutSeconds: number;
    cooldownMs: number;
    categories: number[];
    difficulty: 'easy' | 'medium' | 'hard' | 'random';
}

export interface ModerationConfig {
    prune: {
        maxAmount: number;
        confirmThreshold: number;
        batchSize: number;
        batchDelayMs: number;
    };
}

export interface QuestConfig {
    maxActiveQuests: number;
}

export const gameSettings = pgTable('game_settings', {
    id: text('id').primaryKey().default('default'),
    leveling: jsonb('leveling').$type<LevelingConfig>().notNull(),
    economy: jsonb('economy').$type<EconomyConfig>().notNull(),
    inventory: jsonb('inventory').$type<InventoryConfig>().notNull(),
    lootdrop: jsonb('lootdrop').$type<LootdropConfig>().notNull(),
    trivia: jsonb('trivia').$type<TriviaConfig>().notNull(),
    moderation: jsonb('moderation').$type<ModerationConfig>().notNull(),
    quest: jsonb('quest').$type<QuestConfig>().notNull(),
    commands: jsonb('commands').$type<Record<string, boolean>>().default({}),
    system: jsonb('system').$type<Record<string, unknown>>().default({}),
    createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
    updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
});

export const gameSettingsRelations = relations(gameSettings, () => ({}));
