'use client'

import React, { useState } from 'react'
import { format, parseISO } from 'date-fns'
import { Gutter } from '@payloadcms/ui'
import './styles.scss'

interface MealWithImage {
  id: number
  residentName: string
  residentRoom: string
  status: string
  formImageUrl?: string
  formImageThumbnail?: string
}

interface KitchenReportResponse {
  date: string
  mealType: string
  totalMeals: number
  ingredients: Record<string, number>
  labels: Record<string, string>
  portionSizes?: Record<string, number>
  mealsWithImages?: MealWithImage[]
  error?: string
}

export const KitchenDashboard: React.FC = () => {
  const [date, setDate] = useState(() => format(new Date(), 'yyyy-MM-dd'))
  const [mealType, setMealType] = useState<'breakfast' | 'lunch' | 'dinner'>('breakfast')
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [report, setReport] = useState<KitchenReportResponse | null>(null)

  const generateReport = async () => {
    setLoading(true)
    setError(null)
    setReport(null)

    try {
      const response = await fetch(
        `/api/meals/kitchen-report?date=${date}&mealType=${mealType}`,
        {
          credentials: 'include',
        },
      )

      const data = await response.json()

      if (!response.ok) {
        throw new Error(data.error || 'Failed to generate report')
      }

      setReport(data)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'An error occurred')
    } finally {
      setLoading(false)
    }
  }

  const getMealTypeLabel = (type: string) => {
    switch (type) {
      case 'breakfast':
        return 'Breakfast (Frühstück)'
      case 'lunch':
        return 'Lunch (Mittagessen)'
      case 'dinner':
        return 'Dinner (Abendessen)'
      default:
        return type
    }
  }

  const formatDate = (dateStr: string) => {
    return format(parseISO(dateStr), 'EEEE, MMMM d, yyyy')
  }

  return (
    <Gutter>
      <div className="kitchen-dashboard">
        <header className="kitchen-dashboard__header">
          <h1>Kitchen Dashboard</h1>
          <p>Generate ingredient reports for meal preparation</p>
        </header>

        <section className="kitchen-dashboard__controls">
          <div className="kitchen-dashboard__form">
            <div className="kitchen-dashboard__field">
              <label htmlFor="report-date">Date</label>
              <input
                type="date"
                id="report-date"
                value={date}
                onChange={(e) => setDate(e.target.value)}
                className="kitchen-dashboard__input"
              />
            </div>

            <div className="kitchen-dashboard__field">
              <label htmlFor="meal-type">Meal Type</label>
              <select
                id="meal-type"
                value={mealType}
                onChange={(e) => setMealType(e.target.value as 'breakfast' | 'lunch' | 'dinner')}
                className="kitchen-dashboard__select"
              >
                <option value="breakfast">Breakfast (Frühstück)</option>
                <option value="lunch">Lunch (Mittagessen)</option>
                <option value="dinner">Dinner (Abendessen)</option>
              </select>
            </div>

            <button
              type="button"
              onClick={generateReport}
              disabled={loading}
              className="kitchen-dashboard__button"
            >
              {loading ? 'Generating...' : 'Generate Report'}
            </button>
          </div>
        </section>

        {error && (
          <div className="kitchen-dashboard__error">
            <strong>Error:</strong> {error}
          </div>
        )}

        {report && (
          <section className="kitchen-dashboard__report">
            <div className="kitchen-dashboard__report-header">
              <h2>Ingredient Report</h2>
              <div className="kitchen-dashboard__report-meta">
                <span className="kitchen-dashboard__meta-item">
                  <strong>Date:</strong> {formatDate(report.date)}
                </span>
                <span className="kitchen-dashboard__meta-item">
                  <strong>Meal:</strong> {getMealTypeLabel(report.mealType)}
                </span>
                <span className="kitchen-dashboard__meta-item kitchen-dashboard__meta-item--highlight">
                  <strong>Total Meals:</strong> {report.totalMeals}
                </span>
              </div>
            </div>

            {report.totalMeals === 0 ? (
              <div className="kitchen-dashboard__empty">
                <p>No meals found for this date and meal type.</p>
              </div>
            ) : (
              <>
                {report.portionSizes && Object.keys(report.portionSizes).length > 0 && (
                  <div className="kitchen-dashboard__portion-sizes">
                    <h3>Portion Sizes</h3>
                    <div className="kitchen-dashboard__portion-grid">
                      {Object.entries(report.portionSizes).map(([size, count]) => (
                        <div key={size} className="kitchen-dashboard__portion-item">
                          <span className="kitchen-dashboard__portion-label">
                            {size === 'small'
                              ? 'Small (Kleine)'
                              : size === 'large'
                                ? 'Large (Große)'
                                : 'Vegetarian'}
                          </span>
                          <span className="kitchen-dashboard__portion-count">{count}</span>
                        </div>
                      ))}
                    </div>
                  </div>
                )}

                <div className="kitchen-dashboard__ingredients">
                  <h3>Ingredients Required</h3>
                  {Object.keys(report.ingredients).length === 0 ? (
                    <p className="kitchen-dashboard__no-ingredients">
                      No specific ingredients selected in orders.
                    </p>
                  ) : (
                    <table className="kitchen-dashboard__table">
                      <thead>
                        <tr>
                          <th>Ingredient</th>
                          <th>Quantity</th>
                        </tr>
                      </thead>
                      <tbody>
                        {Object.entries(report.ingredients)
                          .sort(([, a], [, b]) => b - a)
                          .map(([key, count]) => (
                            <tr key={key}>
                              <td>{report.labels[key] || key}</td>
                              <td className="kitchen-dashboard__count">{count}</td>
                            </tr>
                          ))}
                      </tbody>
                      <tfoot>
                        <tr>
                          <td>
                            <strong>Total Items</strong>
                          </td>
                          <td className="kitchen-dashboard__count">
                            <strong>
                              {Object.values(report.ingredients).reduce((a, b) => a + b, 0)}
                            </strong>
                          </td>
                        </tr>
                      </tfoot>
                    </table>
                  )}
                </div>

                {report.mealsWithImages && report.mealsWithImages.length > 0 && (
                  <div className="kitchen-dashboard__form-images">
                    <h3>Paper Form Images</h3>
                    <p className="kitchen-dashboard__form-images-desc">
                      {report.mealsWithImages.length} meal(s) have attached paper form photos
                    </p>
                    <div className="kitchen-dashboard__images-grid">
                      {report.mealsWithImages.map((meal) => (
                        <div key={meal.id} className="kitchen-dashboard__image-card">
                          <div className="kitchen-dashboard__image-header">
                            <strong>{meal.residentName}</strong>
                            <span>Room {meal.residentRoom}</span>
                          </div>
                          {meal.formImageThumbnail && (
                            <a
                              href={meal.formImageUrl}
                              target="_blank"
                              rel="noopener noreferrer"
                              className="kitchen-dashboard__image-link"
                            >
                              <img
                                src={meal.formImageThumbnail}
                                alt={`Form for ${meal.residentName}`}
                                className="kitchen-dashboard__image"
                              />
                              <span className="kitchen-dashboard__image-overlay">
                                Click to view full size
                              </span>
                            </a>
                          )}
                          <div className="kitchen-dashboard__image-status">
                            Status: <span className={`status-${meal.status}`}>{meal.status}</span>
                          </div>
                        </div>
                      ))}
                    </div>
                  </div>
                )}
              </>
            )}
          </section>
        )}
      </div>
    </Gutter>
  )
}

export default KitchenDashboard
