'use client';

import { useState, useCallback } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { AdminUsersTemplate } from '@/templates/AdminUsersTemplate';
import { AdminUsersViewData } from '@/lib/view-data/AdminUsersViewData';
import { updateUserStatus, deleteUser } from '../actions';
import { routes } from '@/lib/routing/RouteConfig';

interface AdminUsersWrapperProps {
  initialViewData: AdminUsersViewData;
}

export function AdminUsersWrapper({ initialViewData }: AdminUsersWrapperProps) {
  const router = useRouter();
  const searchParams = useSearchParams();
  
  // UI state (not business logic)
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [deletingUser, setDeletingUser] = useState<string | null>(null);
  
  // Current filter values from URL
  const search = searchParams.get('search') || '';
  const roleFilter = searchParams.get('role') || '';
  const statusFilter = searchParams.get('status') || '';

  // Callbacks that update URL (triggers RSC re-render)
  const handleSearch = useCallback((newSearch: string) => {
    const params = new URLSearchParams(searchParams);
    if (newSearch) params.set('search', newSearch);
    else params.delete('search');
    params.delete('page'); // Reset to page 1
    router.push(`${routes.admin.users}?${params.toString()}`);
  }, [router, searchParams]);

  const handleFilterRole = useCallback((role: string) => {
    const params = new URLSearchParams(searchParams);
    if (role) params.set('role', role);
    else params.delete('role');
    params.delete('page');
    router.push(`${routes.admin.users}?${params.toString()}`);
  }, [router, searchParams]);

  const handleFilterStatus = useCallback((status: string) => {
    const params = new URLSearchParams(searchParams);
    if (status) params.set('status', status);
    else params.delete('status');
    params.delete('page');
    router.push(`${routes.admin.users}?${params.toString()}`);
  }, [router, searchParams]);

  const handleClearFilters = useCallback(() => {
    router.push(routes.admin.users);
  }, [router]);

  const handleRefresh = useCallback(() => {
    router.refresh();
  }, [router]);

  // Mutation callbacks (call Server Actions)
  const handleUpdateStatus = useCallback(async (userId: string, newStatus: string) => {
    try {
      setLoading(true);
      const result = await updateUserStatus(userId, newStatus);
      
      if (result.isErr()) {
        setError(result.getError());
        return;
      }
      
      // Revalidate data
      router.refresh();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to update status');
    } finally {
      setLoading(false);
    }
  }, [router]);

  const handleDeleteUser = useCallback(async (userId: string) => {
    if (!confirm('Are you sure you want to delete this user? This action cannot be undone.')) {
      return;
    }

    try {
      setDeletingUser(userId);
      const result = await deleteUser(userId);
      
      if (result.isErr()) {
        setError(result.getError());
        return;
      }
      
      // Revalidate data
      router.refresh();
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete user');
    } finally {
      setDeletingUser(null);
    }
  }, [router]);

  return (
    <AdminUsersTemplate
      adminUsersViewData={initialViewData}
      onRefresh={handleRefresh}
      onSearch={handleSearch}
      onFilterRole={handleFilterRole}
      onFilterStatus={handleFilterStatus}
      onClearFilters={handleClearFilters}
      onUpdateStatus={handleUpdateStatus}
      onDeleteUser={handleDeleteUser}
      search={search}
      roleFilter={roleFilter}
      statusFilter={statusFilter}
      loading={loading}
      error={error}
      deletingUser={deletingUser}
    />
  );
}