'use client'

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

// MUI Imports
import Card from '@mui/material/Card'
import Typography from '@mui/material/Typography'
import CardContent from '@mui/material/CardContent'
import CardHeader from '@mui/material/CardHeader'
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 FarmWeatherCardProps {
  data?: Record<string, unknown>
}

const FarmWeatherCard = ({ data }: FarmWeatherCardProps) => {
  const t = useTranslations('farmDashboard')
  const temperature = (data?.temperature as number | undefined) ?? 24
  const condition = (data?.condition as string) ?? ''
  const humidity = (data?.humidity as number | undefined) ?? 45
  const windSpeed = (data?.windSpeed as number | undefined) ?? 12
  const windUnit = (data?.windUnit as string) ?? 'km/h'
  const unit = (data?.unit as string) ?? '°C'
  const chartData = data?.chartData as { labels?: string[]; series?: number[][] } | undefined
  const seriesData = chartData?.series?.[0] ?? [18, 22, 26, 28, 25, 20, 18]
  const series = [{ data: seriesData }]
  const theme = useTheme()
  const infoColor = theme.palette.info.main

  const options: ApexOptions = {
    chart: {
      parentHeightOffset: 0,
      toolbar: { show: false },
      sparkline: { enabled: true }
    },
    tooltip: { enabled: false },
    dataLabels: { enabled: false },
    stroke: {
      width: 2,
      curve: 'smooth'
    },
    grid: {
      show: false,
      padding: { bottom: 20 }
    },
    fill: {
      type: 'gradient',
      gradient: {
        opacityTo: 0,
        opacityFrom: 1,
        shadeIntensity: 1,
        stops: [0, 100],
        colorStops: [
          [
            { offset: 0, opacity: 0.4, color: infoColor },
            { opacity: 0, offset: 100, color: 'var(--mui-palette-background-paper)' }
          ]
        ]
      }
    },
    theme: {
      monochrome: {
        enabled: true,
        shadeTo: 'light',
        shadeIntensity: 1,
        color: infoColor
      }
    },
    xaxis: {
      labels: { show: false },
      axisTicks: { show: false },
      axisBorder: { show: false }
    },
    yaxis: { show: false }
  }

  return (
    <Card className='pbe-6'>
      <CardHeader
        title={t('cards.farmWeatherCard')}
        subheader={condition ? `${condition}, ${temperature}${unit}` : `${temperature}${unit}`}
        className='pbe-3'
        action={<OptionMenu options={[t('optionMenu.refresh'), t('optionMenu.sevenDayForecast'), t('optionMenu.details')]} />}
      />
      <CardContent>
        <div className='flex items-center gap-2 mbe-2'>
          <i className='tabler-sun text-4xl text-warning' />
          <Typography variant='h4'>
            {temperature}
            {unit}
          </Typography>
          <Typography color='text.disabled' variant='body2'>
            {t('labels.humid')}: {humidity}% | {t('labels.wind')}: {windSpeed} {windUnit}
          </Typography>
        </div>
      </CardContent>
      <AppReactApexCharts type='area' height={88} width='100%' series={series} options={options} />
    </Card>
  )
}

export default FarmWeatherCard
