'use client'

// React Imports
import { useTranslations } from 'next-intl'

// Next Imports
import dynamic from 'next/dynamic'

// MUI Imports
import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent'
import Typography from '@mui/material/Typography'
import { useTheme } from '@mui/material/styles'

// Third-party Imports
import type { ApexOptions } from 'apexcharts'

// Component Imports
import OptionMenu from '@core/components/option-menu'

// Styled Component Imports
const AppReactApexCharts = dynamic(() => import('@/libs/styles/AppReactApexCharts'))

interface WaterNeedPredictionProps {
  data?: Record<string, unknown>
}

const WaterNeedPrediction = ({ data }: WaterNeedPredictionProps) => {
  const t = useTranslations('farmDashboard')
  const series = (data?.series as Array<{ name: string; data: number[] }>) ?? []
  const categories =
    (data?.categories as string[]) ??
    [1, 2, 3, 4, 5, 6, 7].map(n => t('fallback.dayN', { n: String(n) }))
  const totalNext7Days = data?.totalNext7Days ?? 0
  const unit = (data?.unit as string) ?? 'm³'
  const totalFormatted = typeof totalNext7Days === 'number' ? totalNext7Days.toLocaleString() : String(totalNext7Days)
  const theme = useTheme()
  const primaryColor = theme.palette.primary.main
  if (series.length === 0) return null

  const options: ApexOptions = {
    chart: {
      parentHeightOffset: 0,
      toolbar: { show: false }
    },
    stroke: { width: 2, curve: 'smooth' },
    colors: [primaryColor],
    fill: {
      type: 'gradient',
      gradient: {
        opacityFrom: 0.5,
        opacityTo: 0.1,
        shadeIntensity: 0.5
      }
    },
    dataLabels: { enabled: false },
    grid: {
      borderColor: 'var(--mui-palette-divider)',
      strokeDashArray: 4,
      xaxis: { lines: { show: false } }
    },
    xaxis: {
      categories,
      labels: { style: { colors: 'var(--mui-palette-text-disabled)' } },
      axisBorder: { show: false },
      axisTicks: { show: false }
    },
    yaxis: {
      labels: {
        style: { colors: 'var(--mui-palette-text-disabled)' },
        formatter: (val: number) => `${val} ${unit}`
      }
    },
    tooltip: {
      y: { formatter: (val: number) => `${val} ${unit}` }
    }
  }

  return (
    <Card>
      <CardHeader
        title={t('cards.waterNeedPrediction')}
        subheader={t('subheaders.aiForecast')}
        action={<OptionMenu options={[t('optionMenu.export'), t('optionMenu.adjust'), t('optionMenu.details')]} />}
      />
      <CardContent>
        <div className='flex items-center gap-4 mbe-4'>
          <Typography variant='h4'>
            {totalFormatted} {unit}
          </Typography>
          <Typography variant='body2' color='text.secondary'>
            {t('labels.totalNext7Days')}
          </Typography>
        </div>
        <AppReactApexCharts type='area' height={250} width='100%' series={series} options={options} />
      </CardContent>
    </Card>
  )
}

export default WaterNeedPrediction
