import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react';
import { useAuth } from '@/contexts/AuthContext';
import { usePezkuwi } from '@/contexts/PezkuwiContext';
import { supabase } from '@/lib/supabase';
import { getAllTikiNFTDetails, generateCitizenNumber, type TikiNFTDetails } from '@pezkuwi/lib/tiki';
import { getKycStatus } from '@pezkuwi/lib/kyc';

interface DashboardData {
  profile: Record<string, unknown> | null | null;
  nftDetails: { citizenNFT: TikiNFTDetails | null; roleNFTs: TikiNFTDetails[]; totalNFTs: number };
  kycStatus: string;
  citizenNumber: string;
  loading: boolean;
}

const DashboardContext = createContext<DashboardData | undefined>(undefined);

export function DashboardProvider({ children }: { children: ReactNode }) {
  const { user } = useAuth();
  const { api, isApiReady, selectedAccount } = usePezkuwi();
  const [profile, setProfile] = useState<Record<string, unknown> | null>(null);
  const [nftDetails, setNftDetails] = useState<{ citizenNFT: TikiNFTDetails | null; roleNFTs: TikiNFTDetails[]; totalNFTs: number }>({
    citizenNFT: null,
    roleNFTs: [],
    totalNFTs: 0
  });
  const [kycStatus, setKycStatus] = useState<string>('NotStarted');
  const [loading, setLoading] = useState(true);

  const fetchProfile = useCallback(async () => {
    if (!user) {
      setLoading(false);
      return;
    }

    try {
      const { data, error } = await supabase
        .from('profiles')
        .select('*')
        .eq('id', user.id)
        .maybeSingle();

      if (error) {
        if (process.env.NODE_ENV !== 'production') console.warn('Profile fetch error (this is normal if Supabase is not configured):', error.message);
        return;
      }

      setProfile(data);
    } catch (error) {
      if (process.env.NODE_ENV !== 'production') console.warn('Error fetching profile (this is normal if Supabase is not configured):', error);
    } finally {
      setLoading(false);
    }
  }, [user]);

  const fetchScoresAndTikis = useCallback(async () => {
    if (!selectedAccount || !api) return;

    setLoading(true);
    try {
      const status = await getKycStatus(api, selectedAccount.address);
      setKycStatus(status);

      const details = await getAllTikiNFTDetails(api, selectedAccount.address);
      setNftDetails(details);
    } catch (error) {
      if (process.env.NODE_ENV !== 'production') console.error('Error fetching data:', error);
    } finally {
      setLoading(false);
    }
  }, [selectedAccount, api]);

  useEffect(() => {
    fetchProfile();
    if (selectedAccount && api && isApiReady) {
      fetchScoresAndTikis();

    }
  }, [user, selectedAccount, api, isApiReady, fetchProfile, fetchScoresAndTikis]);

  const citizenNumber = nftDetails.citizenNFT
    ? generateCitizenNumber(nftDetails.citizenNFT.owner, nftDetails.citizenNFT.collectionId, nftDetails.citizenNFT.itemId)
    : 'N/A';

  return (
    <DashboardContext.Provider value={{ profile, nftDetails, kycStatus, citizenNumber, loading }}>
      {children}
    </DashboardContext.Provider>
  );
}

export function useDashboard() {
  const context = useContext(DashboardContext);
  if (context === undefined) {
    throw new Error('useDashboard must be used within a DashboardProvider');
  }
  return context;
}
