import { createBrowserClient } from '@supabase/ssr'

// Support both EXPO_PUBLIC_ (mobile) and NEXT_PUBLIC_ (web) prefixes
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL 
  || process.env.EXPO_PUBLIC_SUPABASE_URL 
  || 'http://localhost:54321'
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY 
  || process.env.EXPO_PUBLIC_SUPABASE_ANON_KEY 
  || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'

if (typeof window !== 'undefined') {
  console.log('Supabase URL:', supabaseUrl)
  console.log('Using EXPO_PUBLIC:', !!process.env.EXPO_PUBLIC_SUPABASE_URL)
  console.log('Using NEXT_PUBLIC:', !!process.env.NEXT_PUBLIC_SUPABASE_URL)
}

// Use createBrowserClient for proper cookie handling in Next.js
// This stores the session in cookies so middleware can access it
export const supabase = createBrowserClient<Database>(
  supabaseUrl,
  supabaseKey
)

export const isSupabaseConfigured = () => {
  const url = process.env.NEXT_PUBLIC_SUPABASE_URL
  return url !== 'your_supabase_project_url' && url !== 'http://localhost:54321' && !!url
}

export type Json =
  | string
  | number
  | boolean
  | null
  | { [key: string]: Json | undefined }
  | Json[]

export interface Database {
  public: {
    Tables: {
      workouts: {
        Row: {
          id: string
          title: string
          trainer_id: string
          category: 'full-body' | 'core' | 'upper-body' | 'lower-body' | 'cardio'
          level: 'Beginner' | 'Intermediate' | 'Advanced'
          duration: number
          calories: number
          rounds: number
          prep_time: number
          work_time: number
          rest_time: number
          equipment: string[]
          music_vibe: 'electronic' | 'hip-hop' | 'pop' | 'rock' | 'chill'
          exercises: { name: string; duration: number }[]
          thumbnail_url: string | null
          video_url: string | null
          is_featured: boolean
          created_at: string
          updated_at: string
        }
        Insert: {
          id?: string
          title: string
          trainer_id: string
          category: 'full-body' | 'core' | 'upper-body' | 'lower-body' | 'cardio'
          level: 'Beginner' | 'Intermediate' | 'Advanced'
          duration: number
          calories: number
          rounds: number
          prep_time: number
          work_time: number
          rest_time: number
          equipment?: string[]
          music_vibe: 'electronic' | 'hip-hop' | 'pop' | 'rock' | 'chill'
          exercises: { name: string; duration: number }[]
          thumbnail_url?: string | null
          video_url?: string | null
          is_featured?: boolean
        }
        Update: Partial<Database['public']['Tables']['workouts']['Insert']>
      }
      trainers: {
        Row: {
          id: string
          name: string
          specialty: string
          color: string
          avatar_url: string | null
          workout_count: number
          created_at: string
          updated_at: string
        }
        Insert: {
          id?: string
          name: string
          specialty: string
          color: string
          avatar_url?: string | null
          workout_count?: number
        }
        Update: Partial<Omit<Database['public']['Tables']['trainers']['Insert'], 'id'>>
      }
      collections: {
        Row: {
          id: string
          title: string
          description: string
          icon: string
          gradient: string[] | null
          created_at: string
          updated_at: string
        }
        Insert: {
          id?: string
          title: string
          description: string
          icon: string
          gradient?: string[] | null
        }
        Update: Partial<Omit<Database['public']['Tables']['collections']['Insert'], 'id'>>
      }
      collection_workouts: {
        Row: {
          id: string
          collection_id: string
          workout_id: string
          sort_order: number
        }
      }
      admin_users: {
        Row: {
          id: string
          email: string
          role: 'admin' | 'editor'
          created_at: string
          last_login: string | null
        }
      }
    }
  }
}