'use client';

import { useEffect, useState } from 'react';
import { createBrowserClient } from '@/shared/db/supabase-client';
import type { Subscription, PlanLimitCheck } from './types';
import { getPlan } from './plans';

// --- ASA GENERATED START ---
// Billing hooks for client components.
// --- ASA GENERATED END ---

// --- USER CODE START ---

export function useSubscription() {
  const [subscription, setSubscription] = useState<Subscription | null>(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const supabase = createBrowserClient();
    supabase
      .from('subscriptions')
      .select('*')
      .single()
      .then(({ data }) => {
        setSubscription(data as Subscription | null);
        setLoading(false);
      });
  }, []);

  const plan = subscription ? getPlan(subscription.plan) : getPlan('free');

  return { subscription, plan, loading };
}

export function usePlanLimits() {
  const { plan, loading } = useSubscription();

  return {
    limits: plan.limits,
    planName: plan.name,
    loading,
  };
}

// --- USER CODE END ---
