import { Request } from 'express';

// API Response types
export interface ApiResponse<T = any> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
  meta?: PaginationMeta;
}

export interface PaginationMeta {
  page: number;
  limit: number;
  total: number;
  totalPages: number;
}

export interface PaginationParams {
  page: number;
  limit: number;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

// Auth types
export interface JwtPayload {
  userId: string;
  tenantId: string;
  email: string;
  roles: string[];
  sessionId?: string;
  jti?: string;
  iat?: number;
  exp?: number;
}

export interface AuthenticatedRequest extends Request {
  user?: JwtPayload;
  tenantId?: string;
}

// User types (matching auth.users table)
export interface User {
  id: string;
  tenant_id: string;
  email: string;
  password_hash?: string;
  full_name: string;
  status: 'active' | 'inactive' | 'pending' | 'suspended';
  is_superuser: boolean;
  email_verified_at?: Date;
  last_login_at?: Date;
  created_at: Date;
  updated_at: Date;
}

// Role types (matching auth.roles table)
export interface Role {
  id: string;
  tenant_id: string;
  name: string;
  code: string;
  description?: string;
  is_system: boolean;
  color?: string;
  created_at: Date;
}

// Permission types (matching auth.permissions table)
export interface Permission {
  id: string;
  resource: string;
  action: string;
  description?: string;
  module: string;
}

// Tenant types (matching auth.tenants table)
export interface Tenant {
  id: string;
  name: string;
  subdomain: string;
  schema_name: string;
  status: 'active' | 'inactive' | 'suspended';
  settings: Record<string, any>;
  plan: string;
  max_users: number;
  created_at: Date;
}

// Company types (matching auth.companies table)
export interface Company {
  id: string;
  tenant_id: string;
  parent_company_id?: string;
  name: string;
  legal_name?: string;
  tax_id?: string;
  currency_id?: string;
  settings: Record<string, any>;
  created_at: Date;
}

// Error types
export class AppError extends Error {
  constructor(
    public message: string,
    public statusCode: number = 500,
    public code?: string
  ) {
    super(message);
    this.name = 'AppError';
    Error.captureStackTrace(this, this.constructor);
  }
}

export class ValidationError extends AppError {
  constructor(message: string, public details?: any[]) {
    super(message, 400, 'VALIDATION_ERROR');
    this.name = 'ValidationError';
  }
}

export class UnauthorizedError extends AppError {
  constructor(message: string = 'No autorizado') {
    super(message, 401, 'UNAUTHORIZED');
    this.name = 'UnauthorizedError';
  }
}

export class ForbiddenError extends AppError {
  constructor(message: string = 'Acceso denegado') {
    super(message, 403, 'FORBIDDEN');
    this.name = 'ForbiddenError';
  }
}

export class NotFoundError extends AppError {
  constructor(message: string = 'Recurso no encontrado') {
    super(message, 404, 'NOT_FOUND');
    this.name = 'NotFoundError';
  }
}

export class ConflictError extends AppError {
  constructor(message: string = 'Conflicto con recurso existente') {
    super(message, 409, 'CONFLICT');
    this.name = 'ConflictError';
  }
}
