"use client"

import { useState, useEffect } from "react"
import { getDeliveries } from "../services/database"

// Dados fictícios para desenvolvimento
const mockDeliveries = [
  {
    id: "1",
    client: "Supermercado Central",
    address: "Av. Paulista, 1000, São Paulo, SP",
    coordinates: { latitude: -23.5629, longitude: -46.6544 },
    status: "pending",
    scheduled_time: "09:00",
    completed_time: null,
    signature: null,
    photos: [],
    notes: "",
    sync_status: "synced",
  },
  {
    id: "2",
    client: "Farmácia Saúde",
    address: "Rua Augusta, 500, São Paulo, SP",
    coordinates: { latitude: -23.5529, longitude: -46.6426 },
    status: "in_progress",
    scheduled_time: "10:30",
    completed_time: null,
    signature: null,
    photos: [],
    notes: "",
    sync_status: "synced",
  },
  {
    id: "3",
    client: "Restaurante Sabor",
    address: "Rua Oscar Freire, 200, São Paulo, SP",
    coordinates: { latitude: -23.5616, longitude: -46.6709 },
    status: "completed",
    scheduled_time: "08:00",
    completed_time: Date.now() - 3600000,
    signature: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAA...",
    photos: ["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD..."],
    notes: "Entrega realizada com sucesso",
    sync_status: "synced",
  },
  {
    id: "4",
    client: "Loja de Eletrônicos TechStore",
    address: "Av. Brigadeiro Faria Lima, 1500, São Paulo, SP",
    coordinates: { latitude: -23.5673, longitude: -46.6907 },
    status: "pending",
    scheduled_time: "14:00",
    completed_time: null,
    signature: null,
    photos: [],
    notes: "",
    sync_status: "synced",
  },
  {
    id: "5",
    client: "Padaria Pão Quente",
    address: "Rua Consolação, 300, São Paulo, SP",
    coordinates: { latitude: -23.5505, longitude: -46.6522 },
    status: "pending",
    scheduled_time: "15:30",
    completed_time: null,
    signature: null,
    photos: [],
    notes: "",
    sync_status: "synced",
  },
]

// Hook para fornecer dados fictícios ou reais
export const useMockData = () => {
  const [deliveries, setDeliveries] = useState(mockDeliveries)
  const [isLoading, setIsLoading] = useState(true)

  useEffect(() => {
    const loadData = async () => {
      try {
        // Tentar carregar dados reais do banco de dados
        const dbDeliveries = await getDeliveries()

        // Se houver dados no banco, use-os
        if (dbDeliveries && dbDeliveries.length > 0) {
          setDeliveries(dbDeliveries)
        } else {
          // Caso contrário, use dados fictícios
          // Em um ambiente de produção, você pode querer salvar os dados fictícios no banco
          setDeliveries(mockDeliveries)
        }
      } catch (error) {
        console.error("Erro ao carregar dados:", error)
        // Em caso de erro, use dados fictícios
        setDeliveries(mockDeliveries)
      } finally {
        setIsLoading(false)
      }
    }

    loadData()
  }, [])

  // Filtrar entregas por status
  const pendingDeliveries = deliveries.filter((d) => d.status === "pending")
  const inProgressDeliveries = deliveries.filter((d) => d.status === "in_progress")
  const completedDeliveries = deliveries.filter((d) => d.status === "completed")

  // Entregas de hoje (para simplificar, estamos considerando todas as entregas pendentes e em progresso como "de hoje")
  const todayDeliveries = [...pendingDeliveries, ...inProgressDeliveries]

  return {
    deliveries,
    pendingDeliveries,
    inProgressDeliveries,
    completedDeliveries,
    todayDeliveries,
    isLoading,
  }
}

