export interface User {
  id: string
  email: string
  emailVerified: boolean
  createdAt: string
  updatedAt: string
  organizationId?: string
  roleType: UserRole
  isActive: boolean
  lastLoginAt?: string
  metadata?: Record<string, any>
}

export interface Session {
  accessToken: string
  refreshToken: string
  expiresAt: number
  user: User
}

export interface AuthState {
  user: User | null
  session: Session | null
  isLoading: boolean
  isAuthenticated: boolean
  error: AuthError | null
}

export interface AuthError {
  code: string
  message: string
  details?: Record<string, any>
}

export interface AuthCredentials {
  email: string
  password: string
}

export interface SignUpCredentials extends AuthCredentials {
  confirmPassword: string
  organizationId?: string
}

export interface AuthProvider {
  signIn(credentials: AuthCredentials): Promise<AuthResult>
  signUp(credentials: SignUpCredentials): Promise<AuthResult>
  signOut(): Promise<void>
  getSession(): Promise<Session | null>
  refreshSession(): Promise<Session | null>
  resetPassword(email: string): Promise<void>
  updatePassword(password: string): Promise<void>
  onAuthStateChange(callback: (state: AuthState) => void): () => void
}

export interface AuthResult {
  user: User | null
  session: Session | null
  error: AuthError | null
}

export interface AuthConfig {
  provider: AuthProvider
  persistSession: boolean
  autoRefreshToken: boolean
  sessionKey: string
  cookieOptions: CookieOptions
}

export interface CookieOptions {
  httpOnly: boolean
  secure: boolean
  sameSite: 'strict' | 'lax' | 'none'
  path: string
  maxAge?: number
  domain?: string
}

export type UserRole = 'user' | 'admin' | 'super_admin' | 'territory_manager'

export interface RolePermissions {
  canManageEvents: boolean
  canManageUsers: boolean
  canManageOrganization: boolean
  canAccessAdminPanel: boolean
  canManageTerritories: boolean
  canViewAnalytics: boolean
  canManagePayments: boolean
}

export interface AuthContextType {
  state: AuthState
  signIn: (credentials: AuthCredentials) => Promise<AuthResult>
  signUp: (credentials: SignUpCredentials) => Promise<AuthResult>
  signOut: () => Promise<void>
  resetPassword: (email: string) => Promise<void>
  updatePassword: (password: string) => Promise<void>
  refreshSession: () => Promise<Session | null>
  hasPermission: (permission: keyof RolePermissions) => boolean
  hasRole: (role: UserRole) => boolean
  requireAuth: () => void
  requireRole: (role: UserRole) => void
}

export interface AuthGuardOptions {
  requiredRole?: UserRole
  requiredPermissions?: (keyof RolePermissions)[]
  redirectTo?: string
  fallbackComponent?: React.ComponentType
  onUnauthorized?: () => void
}

export interface SessionStorage {
  get(key: string): Promise<string | null>
  set(key: string, value: string): Promise<void>
  remove(key: string): Promise<void>
  clear(): Promise<void>
}