import { DefaultContext, DefaultState } from 'koa';

// 用户相关类型
export interface IUser {
  id: number;
  phone?: string;
  openid?: string;
  nickname: string;
  avatar: string;
  memberLevel: MemberLevel;
  memberExpireAt?: Date;
  dailyUsage: number;
  lastUsageDate: string;
  createdAt: Date;
  updatedAt: Date;
}

export type MemberLevel = -1 | 0 | 1 | 2 | 3 | 4; // -1无限额度(测试) 0免费 1入门 2专业 3旗舰 4企业

// 音频相关类型
export interface IAudio {
  id: number;
  userId: number;
  title: string;
  text: string;
  summary?: string;
  tags: string[];
  audioUrl: string;
  audioDuration: number;
  audioSize: number;
  wordCount: number;
  voiceId: string;
  voiceParams: VoiceParams;
  status: AudioStatus;
  isFavorite: boolean;
  createdAt: Date;
  updatedAt: Date;
}

export interface VoiceParams {
  speed: number; // 0.5 - 2.0
  pitch: number; // -500 - 500
  volume: number; // 0 - 100
  instructText?: string; // Aliyun Instruct 情感/场景控制文本
}

export type AudioStatus = 'processing' | 'completed' | 'failed';

// 订单相关类型
export interface IOrder {
  id: number;
  userId: number;
  orderNo: string;
  productType: ProductType;
  amount: number;
  status: OrderStatus;
  paymentMethod?: string;
  paidAt?: Date;
  createdAt: Date;
  updatedAt: Date;
}

export type ProductType = 'monthly' | 'yearly';
export type OrderStatus = 'pending' | 'paid' | 'failed' | 'refunded';

// 请求相关类型
export interface JwtPayload {
  userId: string;
  phone?: string;
  iat?: number;
  exp?: number;
}

// 扩展 Koa Context
declare module 'koa' {
  interface Context extends DefaultContext {
    state: DefaultState & {
      user?: JwtPayload & {
        memberLevel?: number;
      };
    };
  }
}

// API 响应类型
export interface ApiResponse<T = unknown> {
  code: number;
  message: string;
  data?: T;
}

// 分页类型
export interface PaginationQuery {
  page?: number;
  pageSize?: number;
}

export interface PaginationResult<T> {
  list: T[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// 音色类型
export interface Voice {
  id: string;
  name: string;
  gender: 'male' | 'female';
  description: string;
  preview?: string;
}

// 会员权益
export interface MemberQuota {
  dailyLimit: number; // -1 表示无限制
  wordLimit: number;  // -1 表示无限制
}

export const MEMBER_QUOTA: Record<MemberLevel, MemberQuota> = {
  '-1': { dailyLimit: -1, wordLimit: -1 },
  0: { dailyLimit: 3, wordLimit: 5000 },
  1: { dailyLimit: 10, wordLimit: 10000 },
  2: { dailyLimit: -1, wordLimit: 10000 },
  3: { dailyLimit: -1, wordLimit: -1 },
  4: { dailyLimit: -1, wordLimit: -1 },
};