import React from 'react';
import { PlatformBusiness, PlatformUser } from '../../../api/platform';
import BusinessEditModal from './BusinessEditModal';
import EditPlatformUserModal from './EditPlatformUserModal';

interface EditPlatformEntityModalProps {
    entity: PlatformUser | PlatformBusiness | null;
    type: 'user' | 'business';
    isOpen: boolean;
    onClose: () => void;
}

const EditPlatformEntityModal: React.FC<EditPlatformEntityModalProps> = ({
    entity,
    type,
    isOpen,
    onClose,
}) => {
    if (!isOpen || !entity) return null;

    if (type === 'business') {
        return (
            <BusinessEditModal
                business={entity as PlatformBusiness}
                isOpen={isOpen}
                onClose={onClose}
            />
        );
    }

    if (type === 'user') {
        // Need to cast or transform PlatformUser to the shape expected by EditPlatformUserModal
        // EditPlatformUserModal expects: { id, username, email, first_name, last_name, role, is_active, permissions }
        // PlatformUser has: { id, username, email, name, role, is_active, permissions, ... }
        // We need to split name into first/last if not present
        const user = entity as PlatformUser;

        // Helper to split name if needed (though PlatformUser usually has first/last in backend, the interface might vary)
        // Let's check PlatformUser interface in api/platform.ts
        // It has name?: string, but serializer sends first_name, last_name.
        // Let's assume the object passed in has them.

        return (
            <EditPlatformUserModal
                user={user as any} // Cast to any to avoid strict type mismatch if interfaces slightly differ
                isOpen={isOpen}
                onClose={onClose}
            />
        );
    }

    return null;
};

export default EditPlatformEntityModal;
