import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL === 'your_supabase_project_url' 
  ? 'http://localhost:54321' 
  : (process.env.NEXT_PUBLIC_SUPABASE_URL || 'http://localhost:54321')
const supabaseKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY === 'your_supabase_anon_key'
  ? 'placeholder-key'
  : (process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || 'placeholder-key')

export const supabase = createClient(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
        }
      }
    }
  }
}
