import { jsonb, pgTable, text, timestamp, varchar, integer } from 'drizzle-orm/pg-core';

export const plans = pgTable('billing_plans', {
  id: varchar('id', { length: 32 }).primaryKey(),
  name: varchar('name', { length: 64 }).notNull(),
  priceMonthly: integer('price_monthly').notNull().default(0),
  priceYearly: integer('price_yearly').notNull().default(0),
  currency: varchar('currency', { length: 3 }).notNull().default('RUB'),
  features: jsonb('features').$type<string[]>().default([]),
  limits: jsonb('limits').$type<Record<string, number>>().default({}),
});

export const subscriptions = pgTable('billing_subscriptions', {
  id: text('id').primaryKey(),
  userId: text('user_id').notNull(),
  planId: varchar('plan_id', { length: 32 }).notNull(),
  status: varchar('status', { length: 24 }).notNull().default('active'),
  period: varchar('period', { length: 8 }).notNull().default('monthly'),
  startedAt: timestamp('started_at').defaultNow().notNull(),
  expiresAt: timestamp('expires_at'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
  updatedAt: timestamp('updated_at').defaultNow().notNull(),
});

export const payments = pgTable('billing_payments', {
  id: text('id').primaryKey(),
  userId: text('user_id').notNull(),
  planId: varchar('plan_id', { length: 32 }).notNull(),
  amount: integer('amount').notNull(),
  currency: varchar('currency', { length: 3 }).notNull().default('RUB'),
  status: varchar('status', { length: 24 }).notNull().default('pending'),
  yookassaId: text('yookassa_id'),
  period: varchar('period', { length: 8 }).notNull().default('monthly'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});
