'use client'

// Next Imports
import dynamic from 'next/dynamic'
import { useTranslations } from 'next-intl'

// 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 Grid from '@mui/material/Grid2'
import { useTheme } from '@mui/material/styles'

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

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

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

type EconomicItem = {
  title: string
  value: string
  subtitle: string
  avatarIcon: string
  avatarColor: 'primary' | 'success' | 'info' | 'warning'
}

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

const EconomicOverview = ({ data }: EconomicOverviewProps) => {
  const t = useTranslations('farmDashboard')
  const economicData = (data?.economicData as EconomicItem[] | undefined) ?? []
  const chartSeries = (data?.chartSeries as Array<{ name: string; data: number[] }>) ?? []
  const chartCategories =
    (data?.chartCategories as string[]) ??
    [t('fallback.monthJan'), t('fallback.monthFeb'), t('fallback.monthMar'), t('fallback.monthApr'), t('fallback.monthMay'), t('fallback.monthJun')]
  const theme = useTheme()

  const options: ApexOptions = {
    chart: {
      stacked: true,
      parentHeightOffset: 0,
      toolbar: { show: false }
    },
    legend: { position: 'top', labels: { colors: 'var(--mui-palette-text-secondary)' } },
    dataLabels: { enabled: false },
    stroke: { width: 5, colors: ['var(--mui-palette-background-paper)'] },
    colors: ['var(--mui-palette-primary-main)', 'var(--mui-palette-info-main)'],
    plotOptions: {
      bar: {
        borderRadius: 7,
        columnWidth: '50%',
        borderRadiusApplication: 'around'
      }
    },
    grid: {
      borderColor: 'var(--mui-palette-divider)',
      yaxis: { lines: { show: false } },
      padding: { top: -40, left: -10, right: 0, bottom: -15 }
    },
    xaxis: {
      categories: chartCategories,
      labels: { show: false },
      axisTicks: { show: false },
      axisBorder: { show: false }
    },
    yaxis: { labels: { show: false } }
  }

  return (
    <Card>
      <CardHeader
        title={t('cards.economicOverview')}
        subheader={t('subheaders.costsAndRoi')}
        action={<OptionMenu options={[t('optionMenu.exportPdf'), t('optionMenu.exportExcel'), t('optionMenu.details')]} />}
      />
      <CardContent className='flex flex-col gap-4'>
        {economicData.length > 0 && (
          <Grid container spacing={4}>
            {economicData.map((item, index) => (
              <Grid size={{ xs: 12, sm: 6 }} key={index}>
                <div className='flex items-center gap-4'>
                  <CustomAvatar skin='light' variant='rounded' color={item.avatarColor} size={40}>
                    <i className={classnames(item.avatarIcon, 'text-[22px]')} />
                  </CustomAvatar>
                  <div>
                    <Typography variant='h6'>{item.value}</Typography>
                    <Typography variant='body2' color='text.secondary'>
                      {item.title}
                    </Typography>
                    <Typography variant='caption' color='text.disabled'>
                      {item.subtitle}
                    </Typography>
                  </div>
                </div>
              </Grid>
            ))}
          </Grid>
        )}
        {chartSeries.length > 0 && (
          <AppReactApexCharts type='bar' height={180} width='100%' series={chartSeries} options={options} />
        )}
      </CardContent>
    </Card>
  )
}

export default EconomicOverview
