import EditOutlined from "@mui/icons-material/EditOutlined";
import RefreshOutlined from "@mui/icons-material/RefreshOutlined";
import { Button } from "@/shared/ui/Button.jsx";
import { AdminActionIconButton } from "@/shared/ui/AdminListLayout.jsx";
import { TimeText } from "@/shared/ui/TimeText.jsx";
import styles from "@/features/seven-day-checkin/seven-day-checkin.module.css";

export function SevenDayCheckInConfigSummary({ canUpdate, config, configLoading, onEdit, onRefresh, resourceGroups }) {
    return (
        <section className={styles.summaryPanel}>
            <div className={styles.summaryScrollArea}>
                <div className={styles.summaryContent}>
                    <div className={styles.summaryHeader}>
                        <span className={styles.title}>七日签到配置</span>
                    </div>
                    <div className={styles.summaryItems}>
                        <ConfigStatus config={config} loading={configLoading} />
                        <SummaryItem label="更新时间">
                            {config?.updatedAtMs ? <TimeText value={config.updatedAtMs} /> : configLoading ? "加载中" : "-"}
                        </SummaryItem>
                    </div>
                    <RewardPreview config={config} loading={configLoading} resourceGroups={resourceGroups} />
                </div>
            </div>
            <div className={styles.summaryActions}>
                <AdminActionIconButton disabled={configLoading} label="刷新配置" type="button" onClick={onRefresh}>
                    <RefreshOutlined fontSize="small" />
                </AdminActionIconButton>
                <Button
                    disabled={!canUpdate || configLoading}
                    startIcon={<EditOutlined fontSize="small" />}
                    type="button"
                    variant="primary"
                    onClick={onEdit}
                >
                    编辑配置
                </Button>
            </div>
        </section>
    );
}

function SummaryItem({ children, label }) {
    return (
        <div className={styles.summaryItem}>
            <span className={styles.summaryLabel}>{label}</span>
            <span className={styles.summaryValue}>{children}</span>
        </div>
    );
}

function ConfigStatus({ config, loading }) {
    if (!config) {
        return (
            <span className="status-badge status-badge--stopped">
                <span className="status-point" />
                {loading ? "加载中" : "-"}
            </span>
        );
    }

    return (
        <span
            className={["status-badge", config.enabled ? "status-badge--running" : "status-badge--stopped"].join(" ")}
        >
            <span className="status-point" />
            {config.enabled ? "已开启" : "已关闭"}
        </span>
    );
}

function RewardPreview({ config, loading, resourceGroups }) {
    if (loading && !config) {
        return <div className={styles.rewardPreview}>加载中</div>;
    }
    const rewards = config?.rewards || [];
    return (
        <div className={styles.rewardPreview}>
            {Array.from({ length: 7 }, (_, index) => {
                const dayIndex = index + 1;
                const reward = rewards.find((item) => Number(item.dayIndex) === dayIndex);
                return (
                    <span className={styles.rewardPill} key={dayIndex}>
                        <span>第 {dayIndex} 天</span>
                        <span>{resourceGroupName(resourceGroups, reward?.resourceGroupId)}</span>
                    </span>
                );
            })}
        </div>
    );
}

function resourceGroupName(groups, id) {
    if (!id) {
        return "未配置";
    }
    const group = groups.find((item) => Number(item.groupId) === Number(id));
    return group?.name || group?.groupCode || `#${id}`;
}
