import { useState } from 'react';
import { getDBConnection } from '@/database/sqlite';

export function useCalendar() {
  const [markedDates, setMarkedDates] = useState<any>({});
  const [loading, setLoading] = useState(false);

  const fetchMonthWorkouts = async () => {
    setLoading(true);
    try {
      const db = await getDBConnection();
      const rows: any[] = await db.getAllAsync('SELECT date, type FROM workouts');
      const marks: any = {};
      rows.forEach((w) => {
        marks[w.date] = {
          marked: true,
          dotColor: w.type === 'CYCLING' ? '#4f46e5' : '#10b981',
        };
      });
      setMarkedDates(marks);
    } catch (error) {
      console.error('Lỗi khi tải lịch tập luyện:', error);
    } finally {
      setLoading(false);
    }
  };

  return { markedDates, loading, fetchMonthWorkouts };
}
