import React from 'react';
import { useAuth } from '../contexts/AuthContext';
import { useOrg } from '../contexts/OrgContext';
import { Card } from '../components/UiKit';

const DebugEmployee: React.FC = () => {
    const { user } = useAuth();
    const { employees, org } = useOrg();

    return (
        <div className="min-h-screen bg-[--background-primary] py-8 px-4">
            <div className="max-w-4xl mx-auto">
                <h1 className="text-3xl font-bold text-[--text-primary] mb-8 text-center">
                    Employee Debug Information
                </h1>

                <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    {/* Current User Info */}
                    <Card className="p-6">
                        <h2 className="text-xl font-semibold text-[--text-primary] mb-4">
                            Current User
                        </h2>
                        <div className="space-y-2">
                            <div>
                                <span className="font-medium">Email:</span>
                                <span className="ml-2 text-[--text-secondary]">{user?.email || 'Not logged in'}</span>
                            </div>
                            <div>
                                <span className="font-medium">Display Name:</span>
                                <span className="ml-2 text-[--text-secondary]">{user?.displayName || 'N/A'}</span>
                            </div>
                            <div>
                                <span className="font-medium">UID:</span>
                                <span className="ml-2 text-[--text-secondary] text-xs break-all">{user?.uid || 'N/A'}</span>
                            </div>
                        </div>
                    </Card>

                    {/* Organization Info */}
                    <Card className="p-6">
                        <h2 className="text-xl font-semibold text-[--text-primary] mb-4">
                            Organization
                        </h2>
                        <div className="space-y-2">
                            <div>
                                <span className="font-medium">Name:</span>
                                <span className="ml-2 text-[--text-secondary]">{org?.name || 'Not set'}</span>
                            </div>
                            <div>
                                <span className="font-medium">Org ID:</span>
                                <span className="ml-2 text-[--text-secondary] text-xs break-all">{org?.orgId || 'N/A'}</span>
                            </div>
                            <div>
                                <span className="font-medium">Onboarding Complete:</span>
                                <span className="ml-2 text-[--text-secondary]">{org?.onboardingCompleted ? 'Yes' : 'No'}</span>
                            </div>
                        </div>
                    </Card>

                    {/* Employee Matching Analysis */}
                    <Card className="p-6 lg:col-span-2">
                        <h2 className="text-xl font-semibold text-[--text-primary] mb-4">
                            Employee Matching Analysis
                        </h2>

                        <div className="space-y-4">
                            <div>
                                <span className="font-medium">Total Employees:</span>
                                <span className="ml-2 text-[--text-secondary]">{employees.length}</span>
                            </div>

                            {user?.email && (
                                <div className="space-y-2">
                                    <h3 className="font-medium text-[--text-primary]">Matching Results:</h3>

                                    {/* Exact match */}
                                    <div className="pl-4">
                                        <span className="text-sm font-medium">Exact Email Match:</span>
                                        <span className="ml-2 text-[--text-secondary]">
                                            {employees.find(emp => emp.email === user.email) ? '✅ Found' : '❌ Not found'}
                                        </span>
                                    </div>

                                    {/* Case insensitive match */}
                                    <div className="pl-4">
                                        <span className="text-sm font-medium">Case-Insensitive Match:</span>
                                        <span className="ml-2 text-[--text-secondary]">
                                            {employees.find(emp => emp.email?.toLowerCase() === user.email?.toLowerCase()) ? '✅ Found' : '❌ Not found'}
                                        </span>
                                    </div>

                                    {/* Domain match */}
                                    <div className="pl-4">
                                        <span className="text-sm font-medium">Same Domain Match:</span>
                                        <span className="ml-2 text-[--text-secondary]">
                                            {(() => {
                                                const userDomain = user.email?.split('@')[1];
                                                const domainMatch = employees.find(emp => emp.email?.split('@')[1] === userDomain);
                                                return domainMatch ? `✅ Found: ${domainMatch.name} (${domainMatch.email})` : '❌ Not found';
                                            })()}
                                        </span>
                                    </div>

                                    {/* Username partial match */}
                                    <div className="pl-4">
                                        <span className="text-sm font-medium">Username Partial Match:</span>
                                        <span className="ml-2 text-[--text-secondary]">
                                            {(() => {
                                                const username = user.email?.split('@')[0];
                                                const partialMatch = employees.find(emp =>
                                                    emp.email?.toLowerCase().includes(username?.toLowerCase() || '')
                                                );
                                                return partialMatch ? `✅ Found: ${partialMatch.name} (${partialMatch.email})` : '❌ Not found';
                                            })()}
                                        </span>
                                    </div>
                                </div>
                            )}
                        </div>
                    </Card>

                    {/* All Employees List */}
                    <Card className="p-6 lg:col-span-2">
                        <h2 className="text-xl font-semibold text-[--text-primary] mb-4">
                            All Employees ({employees.length})
                        </h2>

                        <div className="space-y-3">
                            {employees.length === 0 ? (
                                <p className="text-[--text-secondary] italic">No employees found</p>
                            ) : (
                                employees.map((employee, index) => (
                                    <div key={employee.id} className="border border-[--border-color] rounded-lg p-3">
                                        <div className="grid grid-cols-1 md:grid-cols-3 gap-2">
                                            <div>
                                                <span className="font-medium">Name:</span>
                                                <span className="ml-2">{employee.name}</span>
                                            </div>
                                            <div>
                                                <span className="font-medium">Email:</span>
                                                <span className="ml-2 text-sm">{employee.email || 'Not set'}</span>
                                            </div>
                                            <div>
                                                <span className="font-medium">Role:</span>
                                                <span className="ml-2 text-sm">{employee.role || 'Not set'}</span>
                                            </div>
                                        </div>
                                        <div className="mt-2">
                                            <span className="font-medium">ID:</span>
                                            <span className="ml-2 text-xs text-[--text-secondary] break-all">{employee.id}</span>
                                        </div>
                                        {user?.email && employee.email && (
                                            <div className="mt-2 text-sm">
                                                <span className="font-medium">Match Analysis:</span>
                                                <span className="ml-2">
                                                    {employee.email === user.email && (
                                                        <span className="bg-green-100 text-green-800 px-2 py-1 rounded text-xs mr-1">Exact</span>
                                                    )}
                                                    {employee.email?.toLowerCase() === user.email?.toLowerCase() && employee.email !== user.email && (
                                                        <span className="bg-yellow-100 text-yellow-800 px-2 py-1 rounded text-xs mr-1">Case Diff</span>
                                                    )}
                                                    {employee.email?.split('@')[1] === user.email?.split('@')[1] && (
                                                        <span className="bg-blue-100 text-blue-800 px-2 py-1 rounded text-xs mr-1">Same Domain</span>
                                                    )}
                                                    {employee.email?.toLowerCase().includes(user.email?.split('@')[0]?.toLowerCase() || '') && (
                                                        <span className="bg-purple-100 text-purple-800 px-2 py-1 rounded text-xs mr-1">Username Match</span>
                                                    )}
                                                </span>
                                            </div>
                                        )}
                                    </div>
                                ))
                            )}
                        </div>
                    </Card>

                    {/* Quick Actions */}
                    <Card className="p-6 lg:col-span-2">
                        <h2 className="text-xl font-semibold text-[--text-primary] mb-4">
                            Quick Actions
                        </h2>
                        <div className="flex flex-wrap gap-3">
                            <a
                                href="#/employee-questionnaire"
                                className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition-colors text-sm"
                            >
                                Try Traditional Questionnaire
                            </a>
                            <a
                                href="#/employee-questionnaire-steps"
                                className="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 transition-colors text-sm"
                            >
                                Try Stepped Questionnaire
                            </a>
                            <a
                                href="#/reports"
                                className="px-4 py-2 bg-gray-500 text-white rounded hover:bg-gray-600 transition-colors text-sm"
                            >
                                Go to Reports
                            </a>
                            <button
                                onClick={() => window.location.reload()}
                                className="px-4 py-2 bg-orange-500 text-white rounded hover:bg-orange-600 transition-colors text-sm"
                            >
                                Refresh Page
                            </button>
                        </div>
                    </Card>
                </div>
            </div>
        </div>
    );
};

export default DebugEmployee;
