import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { adminPaymentsApi, type SearchStats } from '../api/adminPayments';
import { DateField } from '../components/DateField';
import { useCurrency } from '../hooks/useCurrency';
import type { PendingPayment, PaginatedResponse } from '../types';
import { usePlatform } from '../platform/hooks/usePlatform';
import { StatCard } from '@/components/stats';
import {
  BackIcon,
  SearchIcon,
  CalendarIcon,
  RefreshIcon,
  CheckCircleIcon,
  ChartBarIcon,
  ClockIcon,
  XCircleIcon,
} from '@/components/icons';

interface StatusBadgeProps {
  status: string;
}

function StatusBadge({ status }: StatusBadgeProps) {
  const styles: Record<string, string> = {
    paid: 'bg-success-500/20 text-success-400',
    pending: 'bg-warning-500/20 text-warning-400',
    cancelled: 'bg-error-500/20 text-error-400',
  };

  const normalized = status.toLowerCase();
  const match = Object.keys(styles).find((key) => normalized.includes(key));

  return (
    <span
      className={`rounded-full px-2.5 py-0.5 text-xs font-medium ${match ? styles[match] : 'bg-dark-700/50 text-dark-300'}`}
    >
      {status}
    </span>
  );
}

export default function AdminPayments() {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const { formatAmount, currencySymbol } = useCurrency();
  const { capabilities } = usePlatform();

  const [searchInput, setSearchInput] = useState('');
  const [searchQuery, setSearchQuery] = useState('');
  const [statusFilter, setStatusFilter] = useState<string>('all');
  const [periodFilter, setPeriodFilter] = useState<string>('24h');
  const [dateFrom, setDateFrom] = useState('');
  const [dateTo, setDateTo] = useState('');
  const [showDateRange, setShowDateRange] = useState(false);
  const [methodFilter, setMethodFilter] = useState<string>('');
  const [page, setPage] = useState(1);
  const [checkingPaymentId, setCheckingPaymentId] = useState<string | null>(null);

  // Debounce search input (300ms)
  useEffect(() => {
    const timer = setTimeout(() => {
      setSearchQuery(searchInput);
      setPage(1);
    }, 300);
    return () => clearTimeout(timer);
  }, [searchInput]);

  // Reset page on filter changes
  useEffect(() => {
    setPage(1);
  }, [statusFilter, periodFilter, methodFilter, dateFrom, dateTo]);

  // Auto-refresh only when filters are at defaults and no search
  const isDefaultFilters =
    !searchQuery && statusFilter === 'all' && periodFilter === '24h' && !methodFilter;

  // Shared query params
  const queryParams = {
    search: searchQuery || undefined,
    status_filter: statusFilter,
    method_filter: methodFilter || undefined,
    period: periodFilter === 'custom' ? undefined : periodFilter,
    date_from: periodFilter === 'custom' && dateFrom ? dateFrom : undefined,
    date_to: periodFilter === 'custom' && dateTo ? dateTo : undefined,
  };

  // Fetch payments
  const {
    data: payments,
    isLoading,
    isError,
    refetch,
  } = useQuery<PaginatedResponse<PendingPayment>>({
    queryKey: ['admin-payments-search', queryParams, page],
    queryFn: () =>
      adminPaymentsApi.searchPayments({
        ...queryParams,
        page,
        per_page: 20,
      }),
    refetchInterval: isDefaultFilters ? 30000 : false,
  });

  // Fetch stats
  const { data: stats } = useQuery<SearchStats>({
    queryKey: ['admin-payments-search-stats', queryParams],
    queryFn: () => adminPaymentsApi.getSearchStats(queryParams),
    refetchInterval: isDefaultFilters ? 30000 : false,
  });

  // Check payment mutation
  const checkPaymentMutation = useMutation({
    mutationFn: ({ method, paymentId }: { method: string; paymentId: number }) =>
      adminPaymentsApi.checkPaymentStatus(method, paymentId),
    onSuccess: async () => {
      await refetch();
      queryClient.invalidateQueries({ queryKey: ['admin-payments-search-stats'] });
    },
    onSettled: () => {
      setCheckingPaymentId(null);
    },
  });

  const handleCheckPayment = (payment: PendingPayment) => {
    setCheckingPaymentId(`${payment.method}_${payment.id}`);
    checkPaymentMutation.mutate({ method: payment.method, paymentId: payment.id });
  };

  const handleResetSearch = () => {
    setSearchInput('');
    setSearchQuery('');
    setPage(1);
  };

  const handleStatusCardClick = (status: string) => {
    setStatusFilter(statusFilter === status ? 'all' : status);
  };

  const handlePeriodChange = (period: string) => {
    if (period === 'custom') {
      setPeriodFilter('custom');
      setShowDateRange(true);
    } else {
      setPeriodFilter(period);
      setShowDateRange(false);
    }
  };

  // Get unique methods from stats for filter dropdown
  const methodOptions = stats?.by_method ? Object.keys(stats.by_method) : [];

  // Period filter options
  const periodOptions = [
    { value: '24h', label: t('admin.payments.period24h') },
    { value: '7d', label: t('admin.payments.period7d') },
    { value: '30d', label: t('admin.payments.period30d') },
    { value: 'all', label: t('admin.payments.periodAll') },
  ];

  // Status filter options
  const statusOptions = [
    { value: 'all', label: t('admin.payments.statusAll') },
    { value: 'pending', label: t('admin.payments.statusPending') },
    { value: 'paid', label: t('admin.payments.statusPaid') },
    { value: 'cancelled', label: t('admin.payments.statusCancelled') },
  ];

  return (
    <div className="animate-fade-in space-y-6">
      {/* Header */}
      <div className="flex flex-wrap items-center justify-between gap-4">
        <div className="flex items-center gap-3">
          {/* Show back button only on web, not in Telegram Mini App */}
          {!capabilities.hasBackButton && (
            <button
              onClick={() => navigate('/admin')}
              className="flex h-10 w-10 items-center justify-center rounded-xl border border-dark-700 bg-dark-800 transition-colors hover:border-dark-600"
            >
              <BackIcon />
            </button>
          )}
          <div>
            <h1 className="text-xl font-bold text-dark-100">{t('admin.payments.title')}</h1>
            <p className="text-sm text-dark-400">{t('admin.payments.description')}</p>
          </div>
        </div>
        <button onClick={() => refetch()} className="btn-secondary flex items-center gap-2">
          <RefreshIcon className="h-4 w-4" />
          {t('common.refresh')}
        </button>
      </div>

      {/* Search bar */}
      <div>
        <div className="relative">
          <div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3 text-dark-500">
            <SearchIcon />
          </div>
          <input
            type="text"
            value={searchInput}
            onChange={(e) => setSearchInput(e.target.value)}
            placeholder={t('admin.payments.searchPlaceholder')}
            className="w-full rounded-xl border border-dark-700 bg-dark-800 py-3 pl-10 pr-4 text-dark-100 placeholder-dark-500 transition-colors focus:border-accent-500 focus:outline-none focus:ring-1 focus:ring-accent-500"
          />
        </div>
        <p className="mt-1.5 text-xs text-dark-500">{t('admin.payments.searchHint')}</p>
      </div>

      {/* Search result banner */}
      {searchQuery && (
        <div className="flex items-center justify-between rounded-xl border border-accent-500/30 bg-accent-500/10 px-4 py-3">
          <span className="text-sm text-accent-300">
            {t('admin.payments.searchResults', {
              query: searchQuery,
              count: payments?.total ?? 0,
            })}
          </span>
          <button
            onClick={handleResetSearch}
            className="ml-3 rounded-lg px-3 py-1 text-sm text-accent-400 transition-colors hover:bg-accent-500/20"
          >
            {t('admin.payments.resetSearch')}
          </button>
        </div>
      )}

      {/* Filters row */}
      <div className="space-y-3">
        {/* Status filter pills */}
        <div className="flex flex-wrap items-center gap-2">
          {statusOptions.map((option) => (
            <button
              key={option.value}
              onClick={() => setStatusFilter(option.value)}
              className={`rounded-lg px-3 py-1.5 text-sm transition-all ${
                statusFilter === option.value
                  ? 'bg-accent-500 text-on-accent'
                  : 'bg-dark-800 text-dark-300 hover:bg-dark-700'
              }`}
            >
              {option.label}
            </button>
          ))}
        </div>

        {/* Period filter pills + Method filter */}
        <div className="flex flex-wrap items-center gap-2">
          {periodOptions.map((option) => (
            <button
              key={option.value}
              onClick={() => handlePeriodChange(option.value)}
              className={`rounded-lg px-3 py-1.5 text-sm transition-all ${
                periodFilter === option.value
                  ? 'bg-accent-500 text-on-accent'
                  : 'bg-dark-800 text-dark-300 hover:bg-dark-700'
              }`}
            >
              {option.label}
            </button>
          ))}
          <button
            onClick={() => handlePeriodChange('custom')}
            className={`flex items-center gap-1.5 rounded-lg px-3 py-1.5 text-sm transition-all ${
              periodFilter === 'custom'
                ? 'bg-accent-500 text-on-accent'
                : 'bg-dark-800 text-dark-300 hover:bg-dark-700'
            }`}
          >
            <CalendarIcon className="h-4 w-4" />
            {t('admin.payments.periodCustom')}
          </button>

          {/* Method filter dropdown */}
          {methodOptions.length > 0 && (
            <select
              value={methodFilter}
              onChange={(e) => setMethodFilter(e.target.value)}
              className="rounded-lg border border-dark-700 bg-dark-800 px-3 py-1.5 text-sm text-dark-300 transition-colors focus:border-accent-500 focus:outline-none"
            >
              <option value="">{t('admin.payments.allMethods')}</option>
              {methodOptions.map((method) => (
                <option key={method} value={method}>
                  {method}
                </option>
              ))}
            </select>
          )}
        </div>
      </div>

      {/* Date range panel */}
      {showDateRange && (
        <div className="flex flex-wrap items-end gap-3 rounded-xl border border-accent-500/30 bg-accent-500/5 p-4">
          <div className="flex-1">
            <label className="mb-1 block text-xs text-dark-400">
              {t('admin.payments.dateFrom')}
            </label>
            <DateField
              value={dateFrom}
              max={dateTo}
              onChange={setDateFrom}
              className="flex w-full items-center gap-2 rounded-lg border border-dark-700 bg-dark-800 px-3 py-2 text-sm text-dark-100 transition-colors hover:border-accent-500"
            />
          </div>
          <div className="flex-1">
            <label className="mb-1 block text-xs text-dark-400">{t('admin.payments.dateTo')}</label>
            <DateField
              value={dateTo}
              min={dateFrom}
              onChange={setDateTo}
              className="flex w-full items-center gap-2 rounded-lg border border-dark-700 bg-dark-800 px-3 py-2 text-sm text-dark-100 transition-colors hover:border-accent-500"
            />
          </div>
          <button onClick={() => refetch()} className="btn-primary px-4 py-2 text-sm">
            {t('admin.payments.applyDate')}
          </button>
        </div>
      )}

      {/* Stats cards */}
      {stats && (
        <div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
          <button
            type="button"
            onClick={() => handleStatusCardClick('all')}
            className={`rounded-xl text-left transition-all ${
              statusFilter === 'all' ? 'ring-2 ring-accent-500' : ''
            }`}
          >
            <StatCard
              label={t('admin.payments.totalCount')}
              value={stats.total}
              icon={<ChartBarIcon className="h-5 w-5" />}
              tone="accent"
            />
          </button>
          <button
            type="button"
            onClick={() => handleStatusCardClick('pending')}
            className={`rounded-xl text-left transition-all ${
              statusFilter === 'pending' ? 'ring-2 ring-warning-500' : ''
            }`}
          >
            <StatCard
              label={t('admin.payments.pendingCount')}
              value={stats.pending}
              icon={<ClockIcon className="h-5 w-5" />}
              tone="warning"
            />
          </button>
          <button
            type="button"
            onClick={() => handleStatusCardClick('paid')}
            className={`rounded-xl text-left transition-all ${
              statusFilter === 'paid' ? 'ring-2 ring-success-500' : ''
            }`}
          >
            <StatCard
              label={t('admin.payments.paidCount')}
              value={stats.paid}
              icon={<CheckCircleIcon className="h-5 w-5" />}
              tone="success"
            />
          </button>
          <button
            type="button"
            onClick={() => handleStatusCardClick('cancelled')}
            className={`rounded-xl text-left transition-all ${
              statusFilter === 'cancelled' ? 'ring-2 ring-error-500' : ''
            }`}
          >
            <StatCard
              label={t('admin.payments.cancelledCount')}
              value={stats.cancelled}
              icon={<XCircleIcon className="h-5 w-5" />}
              tone="error"
            />
          </button>
        </div>
      )}

      {/* Payments list */}
      <div className="card">
        {isError ? (
          <div className="py-12 text-center">
            <div className="text-dark-400">{t('common.error')}</div>
            <button onClick={() => refetch()} className="btn-secondary mt-3">
              {t('common.retry')}
            </button>
          </div>
        ) : isLoading ? (
          <div className="flex items-center justify-center py-12">
            <div className="h-8 w-8 animate-spin rounded-full border-2 border-accent-500 border-t-transparent" />
          </div>
        ) : payments?.items && payments.items.length > 0 ? (
          <div className="space-y-3">
            {payments.items.map((payment) => {
              const paymentKey = `${payment.method}_${payment.id}`;
              const isChecking = checkingPaymentId === paymentKey;
              const isCancelled = payment.status.toLowerCase().includes('cancel');

              return (
                <div
                  key={paymentKey}
                  className="rounded-xl border border-dark-700/30 bg-dark-800/30 p-4"
                >
                  <div className="flex flex-wrap items-start justify-between gap-4">
                    <div className="min-w-0 flex-1">
                      {/* Status badge + method */}
                      <div className="mb-2 flex flex-wrap items-center gap-2">
                        <StatusBadge status={payment.status_text} />
                        <span className="font-semibold text-dark-100">
                          {payment.method_display}
                        </span>
                        {payment.is_paid && (
                          <span className="rounded-full bg-success-500/20 px-2 py-0.5 text-xs font-medium text-success-400">
                            {t('admin.payments.paid')}
                          </span>
                        )}
                      </div>

                      {/* Amount */}
                      <div
                        className={`text-lg font-semibold ${
                          isCancelled ? 'text-dark-500 line-through opacity-60' : 'text-dark-50'
                        }`}
                      >
                        {formatAmount(payment.amount_rubles)} {currencySymbol}
                      </div>

                      {/* Invoice ID */}
                      <div className="mt-1 text-sm text-dark-400">
                        <code className="font-mono text-accent-400">{payment.identifier}</code>
                      </div>

                      {/* User info */}
                      {(payment.user_username ||
                        payment.user_telegram_id ||
                        payment.user_email) && (
                        <div className="mt-2 text-sm text-dark-400">
                          <span className="text-dark-500">{t('admin.payments.user')}:</span>{' '}
                          {payment.user_id ? (
                            <button
                              onClick={(e) => {
                                e.stopPropagation();
                                navigate(`/admin/users/${payment.user_id}`);
                              }}
                              className="inline-flex items-center gap-1 transition-colors hover:underline"
                            >
                              {payment.user_username && (
                                <span className="text-accent-400">@{payment.user_username}</span>
                              )}
                              {payment.user_username &&
                                (payment.user_telegram_id || payment.user_email) && (
                                  <span className="text-dark-500"> &middot; </span>
                                )}
                              {payment.user_telegram_id && (
                                <span className="text-accent-300">
                                  TG: {payment.user_telegram_id}
                                </span>
                              )}
                              {!payment.user_telegram_id && payment.user_email && (
                                <span className="text-accent-300">{payment.user_email}</span>
                              )}
                            </button>
                          ) : (
                            <>
                              {payment.user_username && (
                                <span className="text-dark-200">@{payment.user_username}</span>
                              )}
                              {payment.user_username &&
                                (payment.user_telegram_id || payment.user_email) && (
                                  <span className="text-dark-500"> &middot; </span>
                                )}
                              {payment.user_telegram_id && (
                                <span className="text-dark-300">
                                  TG: {payment.user_telegram_id}
                                </span>
                              )}
                              {!payment.user_telegram_id && payment.user_email && (
                                <span className="text-dark-300">{payment.user_email}</span>
                              )}
                            </>
                          )}
                        </div>
                      )}

                      {/* Timestamp */}
                      <div className="mt-1 text-xs text-dark-500">
                        {new Date(payment.created_at).toLocaleString()}
                      </div>
                    </div>

                    {/* Action buttons */}
                    <div className="flex flex-col gap-2">
                      {payment.payment_url && (
                        <a
                          href={payment.payment_url}
                          target="_blank"
                          rel="noopener noreferrer"
                          className="btn-secondary px-3 py-1.5 text-xs"
                        >
                          {t('admin.payments.openLink')}
                        </a>
                      )}
                      {payment.is_checkable && (
                        <button
                          onClick={() => handleCheckPayment(payment)}
                          disabled={isChecking}
                          className="btn-primary px-3 py-1.5 text-xs"
                        >
                          {isChecking ? (
                            <span className="flex items-center gap-1">
                              <svg className="h-3 w-3 animate-spin" viewBox="0 0 24 24" fill="none">
                                <circle
                                  className="opacity-25"
                                  cx="12"
                                  cy="12"
                                  r="10"
                                  stroke="currentColor"
                                  strokeWidth="4"
                                />
                                <path
                                  className="opacity-75"
                                  fill="currentColor"
                                  d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
                                />
                              </svg>
                              {t('admin.payments.checking')}
                            </span>
                          ) : (
                            t('admin.payments.checkStatus')
                          )}
                        </button>
                      )}
                    </div>
                  </div>

                  {/* Show result after check */}
                  {checkPaymentMutation.isSuccess &&
                    checkPaymentMutation.variables?.paymentId === payment.id &&
                    checkPaymentMutation.variables?.method === payment.method && (
                      <div
                        className={`mt-3 rounded-lg p-2 text-sm ${
                          checkPaymentMutation.data?.status_changed
                            ? 'border border-success-500/30 bg-success-500/10 text-success-400'
                            : 'bg-dark-700/30 text-dark-400'
                        }`}
                      >
                        {checkPaymentMutation.data?.message}
                      </div>
                    )}
                  {checkPaymentMutation.isError &&
                    checkPaymentMutation.variables?.paymentId === payment.id &&
                    checkPaymentMutation.variables?.method === payment.method && (
                      <div className="mt-3 rounded-lg border border-error-500/30 bg-error-500/10 p-2 text-sm text-error-400">
                        {t('admin.payments.checkError')}
                      </div>
                    )}
                </div>
              );
            })}
          </div>
        ) : (
          <div className="py-12 text-center">
            <div className="mx-auto mb-4 flex h-16 w-16 items-center justify-center rounded-2xl bg-dark-800">
              <CheckCircleIcon className="h-8 w-8 text-dark-500" />
            </div>
            <div className="text-dark-400">{t('admin.payments.noPayments')}</div>
          </div>
        )}

        {/* Pagination */}
        {payments && payments.pages > 1 && (
          <div className="mt-4 flex flex-wrap items-center gap-3 text-sm text-dark-500">
            <button
              type="button"
              onClick={() => setPage((prev) => Math.max(1, prev - 1))}
              disabled={payments.page <= 1}
              className={`btn-secondary min-w-[100px] flex-1 text-xs sm:flex-none sm:text-sm ${
                payments.page <= 1 ? 'cursor-not-allowed opacity-50' : ''
              }`}
            >
              {t('admin.payments.prev')}
            </button>
            <div className="flex-1 text-center">
              {t('balance.page', {
                current: payments.page,
                total: payments.pages,
              })}
            </div>
            <button
              type="button"
              onClick={() => setPage((prev) => Math.min(payments.pages, prev + 1))}
              disabled={payments.page >= payments.pages}
              className={`btn-secondary min-w-[100px] flex-1 text-xs sm:flex-none sm:text-sm ${
                payments.page >= payments.pages ? 'cursor-not-allowed opacity-50' : ''
              }`}
            >
              {t('admin.payments.next')}
            </button>
          </div>
        )}
      </div>
    </div>
  );
}
