"use client"

import { useEffect, useState } from "react"
import { useRouter } from "next/navigation"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { LogOut } from "lucide-react"
import TranscriptionTable from "@/components/transcription-table"
import ModelPricesTable from "@/components/model-prices-table"
import CostUpdateForm from "@/components/cost-update-form"
import ProductManagement from "@/components/product-management"

export default function Dashboard() {
  const [isAuthenticated, setIsAuthenticated] = useState(false)
  const [isLoading, setIsLoading] = useState(true)
  const router = useRouter()

  useEffect(() => {
    const token = localStorage.getItem("access_token")
    if (!token) {
      router.push("/")
    } else {
      setIsAuthenticated(true)
      setIsLoading(false)
    }
  }, [router])

  const handleLogout = () => {
    localStorage.removeItem("access_token")
    router.push("/")
  }

  if (isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="animate-spin rounded-full h-32 w-32 border-b-2 border-gray-900"></div>
      </div>
    )
  }

  if (!isAuthenticated) {
    return null
  }

  return (
    <div className="min-h-screen bg-gray-50">
      <header className="bg-white shadow">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
          <div className="flex justify-between items-center py-6">
            <h1 className="text-3xl font-bold text-gray-900">Dashboard - Sistema de Transcrição</h1>
            <Button onClick={handleLogout} variant="outline">
              <LogOut className="mr-2 h-4 w-4" />
              Sair
            </Button>
          </div>
        </div>
      </header>

      <main className="max-w-7xl mx-auto py-6 sm:px-6 lg:px-8">
        <Tabs defaultValue="transcription" className="space-y-6">
          <TabsList className="grid w-full grid-cols-3">
            <TabsTrigger value="transcription">Dados de Transcrição</TabsTrigger>
            <TabsTrigger value="models">Preços dos Modelos</TabsTrigger>
            {/* <TabsTrigger value="costs">Atualizar Custos</TabsTrigger> */}
            <TabsTrigger value="products">Produtos</TabsTrigger>
          </TabsList>

          <TabsContent value="transcription">
            <Card>
              <CardHeader>
                <CardTitle>Dados de Transcrição</CardTitle>
                <CardDescription>Visualize e exporte dados de transcrição por cliente ou empresa</CardDescription>
              </CardHeader>
              <CardContent>
                <TranscriptionTable />
              </CardContent>
            </Card>
          </TabsContent>

          <TabsContent value="models">
            <Card>
              <CardHeader>
                <CardTitle>Preços dos Modelos</CardTitle>
                <CardDescription>Gerencie os preços dos modelos de IA disponíveis</CardDescription>
              </CardHeader>
              <CardContent>
                <ModelPricesTable />
              </CardContent>
            </Card>
          </TabsContent>

          <TabsContent value="costs">
            <Card>
              <CardHeader>
                <CardTitle>Atualizar Custos</CardTitle>
                <CardDescription>Atualize os custos de uso dos produtos</CardDescription>
              </CardHeader>
              <CardContent>
                <CostUpdateForm />
              </CardContent>
            </Card>
          </TabsContent>

          <TabsContent value="products">
            <Card>
              <CardHeader>
                <CardTitle>Gerenciar Produtos</CardTitle>
                <CardDescription>Crie, edite e gerencie produtos e seus preços</CardDescription>
              </CardHeader>
              <CardContent>
                <ProductManagement />
              </CardContent>
            </Card>
          </TabsContent>
        </Tabs>
      </main>
    </div>
  )
}
