import os
import pandas as pd
import requests
from typing import List
from supabase import create_client, Client
from procesing.providers.base import DataProvider
from dotenv import load_dotenv

class SupabaseProvider(DataProvider):
    """Concrete Supabase + backend API implementation"""

    def __init__(self,
                 supabase_url: str = None,
                 supabase_key: str = None,):
        load_dotenv()
        self.supabase_url = supabase_url or os.getenv("NEXT_PUBLIC_SUPABASE_URL")
        self.supabase_key = supabase_key or os.getenv("NEXT_PUBLIC_SUPABASE_ANON_KEY")
        self.supabase: Client = create_client(self.supabase_url, self.supabase_key)

    def fetch_products(self, store_mode: str) -> pd.DataFrame:
        # hotel uses room_type, airline uses flight_type; select all and normalize
        resp = self.supabase.table(f'{store_mode}_products').select("*").execute()
        if not resp.data:
            return pd.DataFrame()
        df = pd.DataFrame(resp.data)
        # normalize type column: hotel has room_type, airline has flight_type
        if 'room_type' in df.columns:
            df['product_type'] = df['room_type']
        elif 'flight_type' in df.columns:
            df['product_type'] = df['flight_type']
        return df

    def fetch_experiments(self, experiment_ids: List[str]) -> pd.DataFrame:
        if not experiment_ids:
            return pd.DataFrame()

        resp = self.supabase.table('experiments').select(
            'id, subject_name, xp_human_only, xp_market_mode, xp_task_id, '
            'task:tasks(task_name, task_description, task_def_of_done)'
        ).in_('id', experiment_ids).execute()

        return pd.DataFrame(resp.data) if resp.data else pd.DataFrame()
