import { Head } from '@inertiajs/react'
import { Card, CardContent } from '@ui/components/ui/card'
import { CalendarClock, Target, TrendingUp } from 'lucide-react'
import { FinanceAppLayout } from '@/layouts/finance-app-layout'
import { CashOutChart } from './components/CashOutChart'

interface ChartDataItem {
  month: string
  target: number
  nominal: number
  percentage: number
}

interface DashboardProps {
  title?: string
  chartData: ChartDataItem[]
  summary: {
    total_target: number
    total_nominal: number
  }
}

const formatCurrency = (value: number) =>
  'Rp ' + new Intl.NumberFormat('id-ID').format(value ?? 0)

export default function Dashboard({ title, chartData, summary }: DashboardProps) {
  const overallRatio =
    summary.total_target > 0
      ? (summary.total_nominal / summary.total_target) * 100
      : 0

  return (
    <FinanceAppLayout>
      <Head title={title || 'Dashboard'} />

      <div className='flex h-full flex-1 flex-col gap-4 p-4'>
        {/* Stat Cards */}
        <div className='grid grid-cols-1 gap-4 sm:grid-cols-3'>
          {/* Total Target */}
          <Card className='rounded-2xl border shadow-sm'>
            <CardContent className='p-6'>
              <div className='flex items-start justify-between'>
                <p className='text-xs font-semibold uppercase tracking-widest text-muted-foreground'>
                  Total Target
                </p>
                <div className='flex h-9 w-9 items-center justify-center rounded-full bg-blue-50'>
                  <Target className='h-4 w-4 text-blue-500' />
                </div>
              </div>
              <p className='mt-4 text-3xl font-black tracking-tight text-foreground'>
                {formatCurrency(summary.total_target)}
              </p>
              <p className='mt-1 text-sm text-muted-foreground'>Budget alokasi tahunan</p>
            </CardContent>
          </Card>

          {/* Total Scheduling */}
          <Card className='rounded-2xl border shadow-sm'>
            <CardContent className='p-6'>
              <div className='flex items-start justify-between'>
                <p className='text-xs font-semibold uppercase tracking-widest text-muted-foreground'>
                  Total Scheduling
                </p>
                <div className='flex h-9 w-9 items-center justify-center rounded-full bg-orange-50'>
                  <CalendarClock className='h-4 w-4 text-orange-500' />
                </div>
              </div>
              <p className='mt-4 text-3xl font-black tracking-tight text-foreground'>
                {formatCurrency(summary.total_nominal)}
              </p>
              <p className='mt-1 text-sm text-muted-foreground'>Estimasi pengeluaran terjadwal</p>
            </CardContent>
          </Card>

          {/* Overall Ratio */}
          <Card className='rounded-2xl border-0 bg-gradient-to-br from-blue-600 to-indigo-700 shadow-sm'>
            <CardContent className='p-6'>
              <div className='flex items-start justify-between'>
                <p className='text-xs font-semibold uppercase tracking-widest text-blue-200'>
                  Overall Ratio
                </p>
                <div className='flex h-9 w-9 items-center justify-center rounded-full bg-white/20'>
                  <TrendingUp className='h-4 w-4 text-white' />
                </div>
              </div>
              <p className='mt-4 text-3xl font-black tracking-tight text-white'>
                {overallRatio.toFixed(2)}%
              </p>
              {/* Progress bar */}
              <div className='mt-3 h-1.5 w-full overflow-hidden rounded-full bg-white/30'>
                <div
                  className='h-full rounded-full bg-white'
                  style={{ width: `${Math.min(overallRatio, 100)}%` }}
                />
              </div>
            </CardContent>
          </Card>
        </div>

        {/* Cash Out Chart */}
        <CashOutChart data={chartData} />
      </div>
    </FinanceAppLayout>
  )
}
