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

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

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

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

const SensorComparisonChart = ({ data }: SensorComparisonChartProps) => {
  const t = useTranslations('farmDashboard')
  const series = (data?.series as Array<{ name: string; data: number[] }>) ?? []
  const categories =
    (data?.categories as string[]) ??
    [t('fallback.mon'), t('fallback.tue'), t('fallback.wed'), t('fallback.thu'), t('fallback.fri'), t('fallback.sat'), t('fallback.sun')]
  const currentValue = (data?.currentValue as number | undefined) ?? 48
  const vsLastWeek = (data?.vsLastWeek as string) ?? t('fallback.plusPercentVsLastWeek', { val: '5' })
  const theme = useTheme()
  if (series.length === 0) return null

  const options: ApexOptions = {
    chart: {
      parentHeightOffset: 0,
      toolbar: { show: false },
      zoom: { enabled: false }
    },
    colors: ['var(--mui-palette-primary-main)', 'var(--mui-palette-info-main)'],
    stroke: { width: 2, curve: 'smooth' },
    legend: {
      position: 'top',
      labels: { colors: 'var(--mui-palette-text-secondary)' },
      markers: { offsetX: theme.direction === 'rtl' ? 7 : -4 }
    },
    dataLabels: { enabled: false },
    grid: {
      borderColor: 'var(--mui-palette-divider)',
      strokeDashArray: 4,
      xaxis: { lines: { show: false } },
      yaxis: { lines: { show: true } }
    },
    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}%`
      }
    },
    fill: {
      type: 'gradient',
      gradient: {
        opacityFrom: 0.4,
        opacityTo: 0.05,
        shadeIntensity: 0.5
      }
    }
  }

  return (
    <Card>
      <CardHeader
        title={t('cards.sensorComparisonChart')}
        subheader={t('subheaders.todayVsLastWeek')}
      />
      <CardContent>
        <div className='flex items-center gap-4 mbe-4'>
          <Typography variant='h4'>{currentValue}%</Typography>
          <Typography color='success.main' variant='body2'>
            {vsLastWeek}
          </Typography>
        </div>
        <AppReactApexCharts type='area' height={280} width='100%' series={series} options={options} />
      </CardContent>
    </Card>
  )
}

export default SensorComparisonChart
