import { useEffect, useState } from "react";
import { adminListUsers } from "../../lib/adminApi";
import { getAdminToken } from "../../lib/adminAuth";
import { Box, Typography, Table, TableHead, TableRow, TableCell, TableBody } from "@mui/material";

type Row = {
    id: number;
    username: string;
    email?: string | null;
    created_at?: string;
    is_active?: boolean | null;
};

export default function AdminUsers() {
    const [rows, setRows] = useState<Row[]>([]);
    useEffect(()=>{
        const t = getAdminToken()!;
        adminListUsers(t).then(setRows).catch(console.error);
    }, []);
    return (
        <Box>
            <Typography variant="h6" mb={1}>회원관리</Typography>
            <Table size="small">
                <TableHead>
                    <TableRow>
                        <TableCell>ID</TableCell>
                        <TableCell>아이디</TableCell>
                        <TableCell>이메일</TableCell>
                        <TableCell>가입일</TableCell>
                        <TableCell>활성</TableCell>
                    </TableRow>
                </TableHead>
                <TableBody>
                    {rows.map(r=>(
                        <TableRow key={r.id}>
                            <TableCell>{r.id}</TableCell>
                            <TableCell>{r.username}</TableCell>
                            <TableCell>{r.email ?? "-"}</TableCell>
                            <TableCell>{r.created_at ? new Date(r.created_at).toLocaleString() : "-"}</TableCell>
                            <TableCell>{r.is_active === false ? "N" : "Y"}</TableCell>
                        </TableRow>
                    ))}
                </TableBody>
            </Table>
        </Box>
    );
}
