'use client';

import { useState } from 'react';
import { SponsorSettingsTemplate } from '@/templates/SponsorSettingsTemplate';
import { logoutAction } from '@/app/actions/logoutAction';
import { ConfirmDialog } from '@/components/shared/ConfirmDialog';
import { useRouter } from 'next/navigation';
import { routes } from '@/lib/routing/RouteConfig';
import { logger } from '@/lib/infrastructure/logging/logger';

// ============================================================================
// Mock Data
// ============================================================================

const MOCK_PROFILE = {
  companyName: 'Acme Racing Co.',
  contactName: 'John Smith',
  contactEmail: 'sponsor@acme-racing.com',
  contactPhone: '+1 (555) 123-4567',
  website: 'https://acme-racing.com',
  description: 'Premium sim racing equipment and accessories for competitive drivers. We specialize in high-performance steering wheels, pedals, and cockpit systems used by professionals worldwide.',
  logoUrl: null,
  industry: 'Racing Equipment',
  address: {
    street: '123 Racing Boulevard',
    city: 'Indianapolis',
    country: 'United States',
    postalCode: '46222',
  },
  taxId: 'US12-3456789',
  socialLinks: {
    twitter: '@acmeracing',
    linkedin: 'acme-racing-co',
    instagram: '@acmeracing',
  },
};

const MOCK_NOTIFICATIONS = {
  emailNewSponsorships: true,
  emailWeeklyReport: true,
  emailRaceAlerts: false,
  emailPaymentAlerts: true,
  emailNewOpportunities: true,
  emailContractExpiry: true,
};

const MOCK_PRIVACY = {
  publicProfile: true,
  showStats: false,
  showActiveSponsorships: true,
  allowDirectContact: true,
};

export default function SponsorSettingsPage() {
  const router = useRouter();
  const [profile, setProfile] = useState(MOCK_PROFILE);
  const [notifications, setNotifications] = useState(MOCK_NOTIFICATIONS);
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
  const [isDeleting, setIsDeleting] = useState(false);
  const [saving, setSaving] = useState(false);
  const [saved, setSaved] = useState(false);

  const handleSaveProfile = async () => {
    setSaving(true);
    await new Promise(resolve => setTimeout(resolve, 800));
    logger.info('Profile saved', { profile });
    setSaving(false);
    setSaved(true);
    setTimeout(() => setSaved(false), 3000);
  };

  const handleDeleteAccount = async () => {
    setIsDeleting(true);
    const result = await logoutAction();
    if (result.isErr()) {
      logger.error('Logout failed', new Error(result.getError()));
      setIsDeleting(false);
      return;
    }
    router.push(routes.auth.login);
  };

  const viewData = {
    profile: MOCK_PROFILE,
    notifications: MOCK_NOTIFICATIONS,
    privacy: MOCK_PRIVACY,
  };

  return (
    <>
      <SponsorSettingsTemplate 
        viewData={viewData}
        profile={profile}
        setProfile={setProfile as any}
        notifications={notifications}
        setNotifications={setNotifications as any}
        onSaveProfile={handleSaveProfile}
        onDeleteAccount={() => setIsDeleteDialogOpen(true)}
        saving={saving}
        saved={saved}
      />
      <ConfirmDialog
        isOpen={isDeleteDialogOpen}
        onClose={() => setIsDeleteDialogOpen(false)}
        onConfirm={handleDeleteAccount}
        title="Delete Sponsor Account"
        description="Are you sure you want to delete your sponsor account? This action cannot be undone. All sponsorship data, contracts, and history will be permanently removed."
        confirmLabel="Delete Account"
        variant="danger"
        isLoading={isDeleting}
      />
    </>
  );
}