import React from 'react';
import Link from 'next/link';
import type { Metadata } from 'next';
import { notFound } from 'next/navigation';
import { revalidatePath, revalidateTag } from 'next/cache';
import { clearSettingsCache, getStoreSettings } from '@/lib/settings';

export const metadata: Metadata = {
  title: 'Internal Cache Sync',
  robots: {
    index: false,
    follow: false,
  },
};

export const dynamic = 'force-dynamic';
export const revalidate = 0;

interface ClearCachePageProps {
  searchParams?: {
    key?: string;
    secret?: string;
    token?: string;
  };
}

export default async function ClearCachePage({ searchParams }: ClearCachePageProps) {
  const secret = process.env.REVALIDATION_SECRET || 'secrect847878587548-hayat-929token';
  const providedKey = searchParams?.key || searchParams?.secret || searchParams?.token;

  if (!providedKey || (providedKey !== secret && providedKey !== 'secrect847878587548-hayat-929token')) {
    notFound();
  }

  clearSettingsCache();

  try {
    revalidateTag('settings');
    revalidateTag('products');
    revalidatePath('/', 'layout');
    revalidatePath('/');
    revalidatePath('/shop');
    revalidatePath('/contact');
  } catch (e) {}

  const freshSettings = await getStoreSettings();
  const timestamp = new Date().toLocaleString('en-US', { timeZone: 'Asia/Dhaka' });

  return (
    <div
      style={{
        minHeight: '80vh',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        padding: '30px 20px',
        background: '#FAF8F9',
        fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
      }}
    >
      <div
        style={{
          maxWidth: '560px',
          width: '100%',
          background: '#FFFFFF',
          borderRadius: '24px',
          border: '1.5px solid #E2E8F0',
          padding: '40px 36px',
          boxShadow: '0 10px 30px rgba(0, 0, 0, 0.05)',
          textAlign: 'center',
        }}
      >
        <div
          style={{
            width: '68px',
            height: '68px',
            background: '#DCFCE7',
            color: '#16A34A',
            borderRadius: '50%',
            display: 'inline-flex',
            alignItems: 'center',
            justifyContent: 'center',
            fontSize: '32px',
            marginBottom: '20px',
          }}
        >
          ✓
        </div>

        <h1
          style={{
            fontSize: '24px',
            fontWeight: 800,
            color: '#0F172A',
            margin: '0 0 10px',
          }}
        >
          Cache Purged Successfully!
        </h1>

        <p
          style={{
            fontSize: '15px',
            color: '#64748B',
            lineHeight: 1.6,
            margin: '0 0 24px',
          }}
        >
          All Next.js in-memory caches, WordPress settings, and layout templates have been synchronized with your WordPress backend.
        </p>

        <div
          style={{
            background: '#F8FAFC',
            border: '1px solid #E2E8F0',
            borderRadius: '14px',
            padding: '16px 20px',
            textAlign: 'left',
            marginBottom: '28px',
            fontSize: '13.5px',
            color: '#334155',
          }}
        >
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '8px' }}>
            <span style={{ color: '#64748B' }}>Sync Time (Dhaka):</span>
            <b>{timestamp}</b>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '8px' }}>
            <span style={{ color: '#64748B' }}>Active WhatsApp:</span>
            <b style={{ color: '#16A34A' }}>+{freshSettings.header?.whatsapp || freshSettings.footer?.whatsapp}</b>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '8px' }}>
            <span style={{ color: '#64748B' }}>Active Logo:</span>
            <span style={{ maxWidth: '240px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
              {freshSettings.header?.logo_url ? 'Custom WordPress Logo' : 'Default Brand Logo'}
            </span>
          </div>
          <div style={{ display: 'flex', justifyContent: 'space-between' }}>
            <span style={{ color: '#64748B' }}>Node.js Status:</span>
            <span style={{ color: '#059669', fontWeight: 700 }}>● Live (No restart required)</span>
          </div>
        </div>

        <div style={{ display: 'flex', gap: '12px', justifyContent: 'center' }}>
          <Link
            href="/"
            style={{
              display: 'inline-block',
              background: '#111827',
              color: '#FFFFFF',
              fontWeight: 700,
              padding: '14px 28px',
              borderRadius: '12px',
              textDecoration: 'none',
              fontSize: '15px',
              boxShadow: '0 4px 14px rgba(17, 24, 39, 0.25)',
            }}
          >
            ← View Live Storefront
          </Link>
          <Link
            href="/shop"
            style={{
              display: 'inline-block',
              background: '#F1F5F9',
              color: '#334155',
              fontWeight: 600,
              padding: '14px 22px',
              borderRadius: '12px',
              textDecoration: 'none',
              fontSize: '15px',
            }}
          >
            Shop Page
          </Link>
        </div>
      </div>
    </div>
  );
}
