import Add from "@mui/icons-material/Add";
import DownloadOutlined from "@mui/icons-material/DownloadOutlined";
import GroupsOutlined from "@mui/icons-material/GroupsOutlined";
import PersonRemoveOutlined from "@mui/icons-material/PersonRemoveOutlined";
import ShieldOutlined from "@mui/icons-material/ShieldOutlined";
import VerifiedUserOutlined from "@mui/icons-material/VerifiedUserOutlined";
import { DataState } from "@/shared/ui/DataState.jsx";
import {
    AdminActionIconButton,
    AdminListBody,
    AdminListPage,
    AdminListToolbar,
} from "@/shared/ui/AdminListLayout.jsx";
import { UserFormDrawer } from "@/features/users/components/UserFormDrawer.jsx";
import { UserProfileDrawer } from "@/features/users/components/UserProfileDrawer.jsx";
import { UserTable } from "@/features/users/components/UserTable.jsx";
import { useUsersPage } from "@/features/users/hooks/useUsersPage.js";
import { userStatusFilters } from "@/features/users/constants.js";

export function UserManagementPage() {
    const page = useUsersPage();

    return (
        <>
            <AdminListPage>
                <AdminListToolbar
                    actions={
                        <>
                            {page.abilities.canViewRoles ? (
                                <AdminActionIconButton label="角色配置" onClick={() => page.navigate("/system/roles")}>
                                    <ShieldOutlined fontSize="small" />
                                </AdminActionIconButton>
                            ) : null}
                            {page.abilities.canViewTeams ? (
                                <AdminActionIconButton label="团队配置" onClick={() => page.navigate("/system/teams")}>
                                    <GroupsOutlined fontSize="small" />
                                </AdminActionIconButton>
                            ) : null}
                            {page.abilities.canStatus ? (
                                <>
                                    <AdminActionIconButton label="批量启用" onClick={() => page.batchStatus("active")}>
                                        <VerifiedUserOutlined fontSize="small" />
                                    </AdminActionIconButton>
                                    <AdminActionIconButton
                                        label="批量停用"
                                        tone="danger"
                                        onClick={() => page.batchStatus("disabled")}
                                    >
                                        <PersonRemoveOutlined fontSize="small" />
                                    </AdminActionIconButton>
                                </>
                            ) : null}
                            {page.abilities.canExport ? (
                                <AdminActionIconButton label="导出" onClick={page.downloadUsers}>
                                    <DownloadOutlined fontSize="small" />
                                </AdminActionIconButton>
                            ) : null}
                            {page.abilities.canCreate ? (
                                <AdminActionIconButton label="新增用户" primary onClick={page.openCreate}>
                                    <Add fontSize="small" />
                                </AdminActionIconButton>
                            ) : null}
                        </>
                    }
                />

                <DataState error={page.error} loading={page.loading} onRetry={page.reload}>
                    <AdminListBody>
                        <UserTable
                            canEdit={page.abilities.canEdit}
                            canResetPassword={page.abilities.canResetPassword}
                            canStatus={page.abilities.canStatus}
                            onEdit={page.openEdit}
                            onOpenProfile={page.openProfile}
                            onResetPassword={page.resetPassword}
                            onToggleDisabled={page.toggleDisabled}
                            onToggleLocked={page.toggleLocked}
                            query={page.query}
                            roleFilter={page.roleFilter}
                            roleFilters={page.roleFilters}
                            selectedIds={page.selectedIds}
                            setSelectedIds={page.setSelectedIds}
                            statusFilter={page.statusFilter}
                            statusFilters={userStatusFilters}
                            onQueryChange={page.changeQuery}
                            onRoleFilterChange={page.changeRoleFilter}
                            onStatusFilterChange={page.changeStatusFilter}
                            pagination={{
                                page: page.page,
                                pageSize: page.users.pageSize || 50,
                                total: page.users.total || 0,
                                onPageChange: page.setPage,
                            }}
                            users={page.users.items || []}
                        />
                    </AdminListBody>
                </DataState>
            </AdminListPage>

            <UserFormDrawer
                appScopeCatalog={page.appScopeCatalog}
                editingUser={page.editingUser}
                financeScopeAssignments={page.financeScopeAssignments}
                financeScopeCatalog={page.financeScopeCatalog}
                form={page.form}
                onClose={page.closeForm}
                onAppScopeChecked={page.setAppScopeChecked}
                onAppScopeModeChange={page.setAppScopeMode}
                onFinanceScopeChecked={page.setFinanceScopeChecked}
                onRoleChecked={page.setRoleChecked}
                onSubmit={page.submitUser}
                open={page.formOpen}
                roles={page.roles}
                setForm={page.setForm}
                teams={page.teams}
            />
            <UserProfileDrawer onClose={page.closeProfile} open={Boolean(page.profileUser)} user={page.profileUser} />
        </>
    );
}
