import { pgTable, text, timestamp, uuid, boolean, integer } from 'drizzle-orm/pg-core';
import { CREDIT_CONFIG } from '@/lib/constants';

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom(),
  email: text('email').notNull().unique(),
  password: text('password'),
  username: text('username'),
  emailVerified: timestamp('email_verified'),
  image: text('image'),
  isEmailVerified: boolean('is_email_verified').notNull().default(false),
  credits: integer('credits').notNull().default(CREDIT_CONFIG.REGISTRATION_BONUS), // 永久积分
  subscriptionCredits: integer('subscription_credits').notNull().default(0), // 订阅积分
  // 订阅相关字段
  subscriptionStatus: text('subscription_status').default('none'), // 'none', 'active', 'canceled', 'expired'
  subscriptionPlan: text('subscription_plan'), // 'pro', 'enterprise', etc.
  subscriptionStartDate: timestamp('subscription_start_date'),
  subscriptionEndDate: timestamp('subscription_end_date'),
  stripeCustomerId: text('stripe_customer_id'),
  stripeSubscriptionId: text('stripe_subscription_id'),
  createdAt: timestamp('created_at').defaultNow(),
  updatedAt: timestamp('updated_at').defaultNow(),
});

export const verificationTokens = pgTable('verification_tokens', {
  id: uuid('id').primaryKey().defaultRandom(),
  email: text('email').notNull(),
  token: text('token').notNull().unique(),
  expires: timestamp('expires').notNull(),
  type: text('type').notNull(), // 'email_verification' | 'password_reset'
  createdAt: timestamp('created_at').defaultNow(),
});

export const accounts = pgTable('accounts', {
  id: uuid('id').primaryKey().defaultRandom(),
  userId: uuid('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
  type: text('type').notNull(),
  provider: text('provider').notNull(),
  providerAccountId: text('provider_account_id').notNull(),
  refresh_token: text('refresh_token'),
  access_token: text('access_token'),
  expires_at: integer('expires_at'),
  token_type: text('token_type'),
  scope: text('scope'),
  id_token: text('id_token'),
  session_state: text('session_state'),
});

export const sessions = pgTable('sessions', {
  id: uuid('id').primaryKey().defaultRandom(),
  sessionToken: text('session_token').notNull().unique(),
  userId: uuid('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
  expires: timestamp('expires').notNull(),
});

// 活动记录表
export const userActivities = pgTable('user_activities', {
  id: uuid('id').primaryKey().defaultRandom(),
  userId: uuid('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }),
  type: text('type').notNull(), // 'credit_deduct', 'credit_add', 'image_generation', 'login', etc.
  description: text('description').notNull(),
  creditAmount: integer('credit_amount'), // 积分变化数量，负数为扣除，正数为增加
  metadata: text('metadata'), // JSON格式的额外数据
  createdAt: timestamp('created_at').defaultNow(),
});

export type User = typeof users.$inferSelect;
export type NewUser = typeof users.$inferInsert;
export type VerificationToken = typeof verificationTokens.$inferSelect;
export type NewVerificationToken = typeof verificationTokens.$inferInsert; 