import { useEffect, useState } from 'react'
import { Link, useParams, useNavigate } from 'react-router-dom'
import { SearchableSelect } from '../components/SearchableSelect'
import { ImageCropper } from '../components/ImageCropper'
import { ImageUploader } from '../components/ImageUploader'
import { TagInput } from '../components/TagInput'
import { RichTextEditor } from '../components/RichTextEditor'
import { Breadcrumbs } from '../components/Breadcrumbs'
import { useToast } from '../context/ToastContext'
import { useT } from '../i18n/useT'
import { ApiError } from '../lib/api'
import {
  listPortfolioCategories,
  mapPortfolioCategoryToUi,
} from '../services/portfolioCategoryService'
import {
  resolveDataUrlToMediaId,
  resolveDataUrlsToMediaIds,
  resolveHtmlEmbeddedDataUrls,
} from '../services/mediaService'
import {
  createPortfolio,
  getPortfolio,
  mapPortfolioApiToFormState,
  updatePortfolio,
} from '../services/portfolioService'
import type { Category } from '../types/category'
import { flattenCategories } from '../utils/categories'
import pageStyles from '../components/PageContent.module.css'
import styles from './AddNewProductPage.module.css'

type TitleImageAspectId = 'square' | '3:2' | '16:9' | '9:16'

const TITLE_IMAGE_ASPECTS: {
  id: TitleImageAspectId
  aspect: number
}[] = [
  { id: 'square', aspect: 1 },
  { id: '3:2', aspect: 3 / 2 },
  { id: '16:9', aspect: 16 / 9 },
  { id: '9:16', aspect: 9 / 16 },
]

export function AddNewPortfolioPage() {
  const { id } = useParams()
  const navigate = useNavigate()
  const { showToast } = useToast()
  const t = useT()
  const isEdit = Boolean(id)

  const [categories, setCategories] = useState<Category[]>([])
  const [categoryId, setCategoryId] = useState('')
  const [titleFa, setTitleFa] = useState('')
  const [titleEn, setTitleEn] = useState('')
  const [abstract, setAbstract] = useState('')
  const [mainTextHtml, setMainTextHtml] = useState('')
  const [titleImage, setTitleImage] = useState<string | null>(null)
  const [titleImageAspect, setTitleImageAspect] = useState<TitleImageAspectId>('3:2')
  const [images, setImages] = useState<string[]>([])
  const [tags, setTags] = useState<string[]>([])
  const [featuredMediaId, setFeaturedMediaId] = useState<string | null>(null)
  const [galleryMediaIds, setGalleryMediaIds] = useState<string[]>([])
  const [loaded, setLoaded] = useState(!isEdit)
  const [isSubmitting, setIsSubmitting] = useState(false)
  const [error, setError] = useState('')

  const categoryOptions = flattenCategories(categories)
  const selectedAspect =
    TITLE_IMAGE_ASPECTS.find((option) => option.id === titleImageAspect)?.aspect ?? 3 / 2

  useEffect(() => {
    const controller = new AbortController()

    async function loadCategories() {
      try {
        const items = await listPortfolioCategories(controller.signal)
        setCategories(items.map(mapPortfolioCategoryToUi))
      } catch (err) {
        if (err instanceof DOMException && err.name === 'AbortError') return
        if (err instanceof ApiError) {
          setError(err.message)
        } else {
          setError(t('portfolio.categories.errorLoad'))
        }
      }
    }

    void loadCategories()
    return () => controller.abort()
  }, [])

  useEffect(() => {
    if (!isEdit || !id) return

    const controller = new AbortController()

    async function loadPortfolio() {
      setError('')
      try {
        const portfolio = await getPortfolio(id!, controller.signal)
        const form = mapPortfolioApiToFormState(portfolio)
        setCategoryId(form.categoryId)
        setTitleFa(form.titleFa)
        setTitleEn(form.titleEn)
        setAbstract(form.abstract)
        setMainTextHtml(form.mainTextHtml)
        setTitleImage(form.titleImage)
        setImages(form.images)
        setTags(form.tags)
        setFeaturedMediaId(form.featuredMediaId)
        setGalleryMediaIds(form.galleryMediaIds)
        setLoaded(true)
      } catch (err) {
        if (err instanceof DOMException && err.name === 'AbortError') return
        navigate('/portfolios/list', { replace: true })
      }
    }

    void loadPortfolio()
    return () => controller.abort()
  }, [isEdit, id, navigate])

  if (!loaded) return null

  async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
    e.preventDefault()
    setIsSubmitting(true)
    setError('')

    try {
      const nextFeaturedMediaId = await resolveDataUrlToMediaId(
        titleImage,
        'portfolio-title-image.jpg',
        featuredMediaId,
      )
      const resolvedMainTextHtml = await resolveHtmlEmbeddedDataUrls(mainTextHtml)
      const nextGalleryMediaIds = await resolveDataUrlsToMediaIds(images, galleryMediaIds)

      if (titleImage?.startsWith('data:') && !nextFeaturedMediaId) {
        setError('Unable to upload title image. Please try again.')
        setIsSubmitting(false)
        return
      }

      const payload = {
        titleFa: titleFa.trim(),
        titleEn: titleEn.trim() || undefined,
        abstract: abstract.trim(),
        mainTextHtml: resolvedMainTextHtml,
        categoryId: categoryId || undefined,
        featuredMediaId: nextFeaturedMediaId ?? undefined,
        galleryMediaIds: nextGalleryMediaIds,
        tags,
        status: 'published' as const,
      }

      if (isEdit && id) {
        await updatePortfolio(id, {
          ...payload,
          categoryId: categoryId || null,
          featuredMediaId: nextFeaturedMediaId,
        })
        showToast(t('portfolio.form.toast.updated'), 'success')
      } else {
        await createPortfolio(payload)
        showToast(t('portfolio.form.toast.created'), 'success')
      }

      navigate('/portfolios/list')
    } catch (err) {
      if (err instanceof ApiError) {
        setError(err.message)
      } else if (err instanceof Error) {
        setError(err.message)
      } else {
        setError(t('portfolio.form.errorSave'))
      }
    } finally {
      setIsSubmitting(false)
    }
  }

  return (
    <main className={pageStyles.content}>
      <Breadcrumbs
        items={
          isEdit
            ? [
                { label: 'Dashboard', href: '/' },
                { label: 'Portfolios', href: '/portfolios' },
                { label: 'My Portfolios', href: '/portfolios/list' },
                { label: 'Edit Portfolio' },
              ]
            : [
                { label: 'Dashboard', href: '/' },
                { label: 'Portfolios', href: '/portfolios' },
                { label: 'Add New Portfolio' },
              ]
        }
      />
      <div className={pageStyles.pageHeader}>
        <div>
          <h2 className={pageStyles.pageTitle}>
            {isEdit ? t('title.editPortfolio') : t('title.addPortfolio')}
          </h2>
          <p className={pageStyles.pageSubtitle}>
            {isEdit ? t('portfolio.form.edit.subtitle') : t('portfolio.form.add.subtitle')}
          </p>
        </div>
      </div>

      {error && (
        <div className={styles.error} role="alert">
          {error}
        </div>
      )}

      <form className={styles.form} onSubmit={handleSubmit}>
        <div className={styles.formGrid}>
          <div className={`${styles.field} ${styles.col3} ${styles.thumbnailField}`}>
            <label>{t('portfolio.form.titleImage')}</label>
            <div
              className={styles.aspectOptions}
              role="radiogroup"
              aria-label={t('portfolio.form.aspectAria')}
            >
              {TITLE_IMAGE_ASPECTS.map((option) => (
                <button
                  key={option.id}
                  type="button"
                  role="radio"
                  aria-checked={titleImageAspect === option.id}
                  className={`${styles.aspectChip} ${
                    titleImageAspect === option.id ? styles.aspectChipSelected : ''
                  }`}
                  onClick={() => {
                    if (option.id === titleImageAspect) return
                    setTitleImageAspect(option.id)
                    // Aspect only applies during crop — clear so the user re-crops
                    if (titleImage) {
                      setTitleImage(null)
                      setFeaturedMediaId(null)
                    }
                  }}
                  disabled={isSubmitting}
                >
                  {option.id === 'square'
                    ? t('portfolio.form.aspect.square')
                    : option.id === '9:16'
                      ? t('portfolio.form.aspect.reel')
                      : option.id}
                </button>
              ))}
            </div>
            <ImageCropper
              value={titleImage}
              onChange={setTitleImage}
              aspect={selectedAspect}
              uploadLabel={t('portfolio.form.uploadTitleImage')}
              hint={t('portfolio.form.cropHint')}
              changeLabel={t('portfolio.form.changeTitleImage')}
            />
          </div>

          <div className={styles.formColumn}>
            <div className={styles.field}>
              <label>{t('portfolio.form.category')}</label>
              <SearchableSelect
                options={categoryOptions}
                value={categoryId}
                onChange={setCategoryId}
                placeholder={t('portfolio.form.categoryPlaceholder')}
              />
            </div>

            <div className={styles.titleRow}>
              <div className={styles.field}>
                <label htmlFor="portfolio-title-fa">{t('portfolio.form.titleFa')}</label>
                <input
                  id="portfolio-title-fa"
                  name="titleFa"
                  type="text"
                  dir="rtl"
                  lang="fa"
                  className="faText"
                  placeholder={t('portfolio.form.titleFaPlaceholder')}
                  value={titleFa}
                  onChange={(e) => setTitleFa(e.target.value)}
                  required
                  disabled={isSubmitting}
                />
              </div>

              <div className={styles.field}>
                <label htmlFor="portfolio-title-en">{t('portfolio.form.titleEn')}</label>
                <input
                  id="portfolio-title-en"
                  name="titleEn"
                  type="text"
                  dir="ltr"
                  placeholder={t('portfolio.form.titleEnPlaceholder')}
                  value={titleEn}
                  onChange={(e) => setTitleEn(e.target.value)}
                  disabled={isSubmitting}
                />
              </div>
            </div>

            <div className={styles.field}>
              <label htmlFor="portfolio-abstract">{t('portfolio.form.abstract')}</label>
              <textarea
                id="portfolio-abstract"
                name="abstract"
                rows={4}
                placeholder={t('portfolio.form.abstractPlaceholder')}
                value={abstract}
                onChange={(e) => setAbstract(e.target.value)}
                required
                disabled={isSubmitting}
              />
            </div>

            <div className={styles.field}>
              <label>{t('portfolio.form.mainText')}</label>
              <RichTextEditor
                value={mainTextHtml}
                onChange={setMainTextHtml}
                placeholder={t('portfolio.form.mainTextPlaceholder')}
                allowImages
                editorMinHeight={280}
              />
            </div>
          </div>

          <div className={`${styles.field} ${styles.col12}`}>
            <label>{t('portfolio.form.gallery')}</label>
            <ImageUploader images={images} onChange={setImages} />
          </div>

          <div className={`${styles.field} ${styles.col12}`}>
            <label>{t('portfolio.form.tags')}</label>
            <TagInput tags={tags} onChange={setTags} />
          </div>
        </div>

        <div className={styles.actions}>
          <Link to="/portfolios/list" className={styles.cancelBtn}>
            {t('portfolio.form.cancel')}
          </Link>
          <button type="submit" className={styles.submitBtn} disabled={isSubmitting}>
            {isSubmitting
              ? t('portfolio.form.saving')
              : isEdit
                ? t('portfolio.form.update')
                : t('portfolio.form.save')}
          </button>
        </div>
      </form>
    </main>
  )
}
