// src/pages/admin/CorrectionsPage.tsx
import React, { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { getSuggestedCorrections, fetchMasterItems, fetchCategories } from '../../services/apiClient'; // Using apiClient for all data fetching
import { logger } from '../../services/logger.client';
import type { SuggestedCorrection, MasterGroceryItem, Category } from '../../types';
import { LoadingSpinner } from '../../components/LoadingSpinner';
import { ArrowPathIcon } from '../../components/icons/ArrowPathIcon';
import { CorrectionRow } from './components/CorrectionRow';

export const CorrectionsPage: React.FC = () => {
    const [corrections, setCorrections] = useState<SuggestedCorrection[]>([]);
    const [isLoading, setIsLoading] = useState(true);
    const [masterItems, setMasterItems] = useState<MasterGroceryItem[]>([]);
    const [categories, setCategories] = useState<Category[]>([]);
    const [error, setError] = useState<string | null>(null);

    const fetchCorrections = async () => {
        setIsLoading(true);
        setError(null);
        try {
            // Fetch all required data in parallel for efficiency
            const [correctionsResponse, masterItemsResponse, categoriesResponse] = await Promise.all([
                getSuggestedCorrections(),
                fetchMasterItems(),
                fetchCategories()
            ]);
            setCorrections(await correctionsResponse.json());
            setMasterItems(await masterItemsResponse.json());
            setCategories(await categoriesResponse.json());
        } catch (err) {
            logger.error('Failed to fetch corrections', err);
            const errorMessage = err instanceof Error ? err.message : 'An unknown error occurred while fetching corrections.';
            setError(errorMessage);
        } finally {
            setIsLoading(false);
        }
    };

    useEffect(() => {
        fetchCorrections();
    }, []);

    const handleCorrectionProcessed = (correctionId: number) => {
        setCorrections(prev => prev.filter(c => c.suggested_correction_id !== correctionId));
    };

    return (
        <div className="max-w-7xl mx-auto py-8 px-4">
            <div className="mb-8">
                <Link to="/admin" className="text-brand-primary hover:underline">&larr; Back to Admin Dashboard</Link>
                <div className="flex justify-between items-center mt-2">
                    <div>
                        <h1 className="text-3xl font-bold text-gray-800 dark:text-white">User-Submitted Corrections</h1>
                        <p className="text-gray-500 dark:text-gray-400">Review and approve or reject pending data corrections.</p>
                    </div>
                    <button 
                        onClick={fetchCorrections} 
                        disabled={isLoading}
                        className="p-2 rounded-md bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 disabled:opacity-50"
                        title="Refresh Corrections"
                    >
                        <ArrowPathIcon className={`w-5 h-5 ${isLoading ? 'animate-spin' : ''}`} />
                    </button>
                </div>
            </div>

            {isLoading && <div role="status" aria-label="Loading corrections" className="flex justify-center items-center h-64"><LoadingSpinner /></div>}
            {error && <div className="text-red-500 bg-red-100 dark:bg-red-900/20 p-4 rounded-lg">{error}</div>}

            {!isLoading && !error && (
                <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-x-auto">
                    <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                        <thead className="bg-gray-50 dark:bg-gray-700/50">
                            <tr>
                                <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Item</th>
                                <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Correction Type</th>
                                <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Suggested Value</th>
                                <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Submitted By</th>
                                <th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">Date</th>
                                <th scope="col" className="relative px-6 py-3"><span className="sr-only">Actions</span></th>
                            </tr>
                        </thead>
                        <tbody className="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
                            {corrections.length === 0 ? (
                                <tr><td colSpan={6} className="text-center py-8 text-gray-500">No pending corrections. Great job!</td></tr>
                            ) : (
                                corrections.map(correction => (
                                    <CorrectionRow 
                                        key={correction.suggested_correction_id} 
                                        correction={correction} 
                                        masterItems={masterItems}
                                        categories={categories}
                                        onProcessed={handleCorrectionProcessed} />
                                ))
                            )}
                        </tbody>
                    </table>
                </div>
            )}
        </div>
    );
};