"use client";

import { useState, type ReactNode } from "react";

import Box from "@mui/material/Box";
import ButtonBase from "@mui/material/ButtonBase";
import Dialog from "@mui/material/Dialog";
import DialogContent from "@mui/material/DialogContent";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import { alpha, useTheme } from "@mui/material/styles";

import { dashboardAssets } from "@/components/dashboard/dashboard-assets";
import { DashboardAssetIcon } from "@/components/dashboard/dashboard-asset-icon";
import { DocumentLink } from "@/components/ui/document-link";
import { DocumentFileAttachment } from "@/components/ui/document-file-name";
import {
    formatCalendarDate,
    formatCalendarTimeRange,
} from "@/components/ui/month-calendar";
import type {
    MeetingCalendarEvent,
    MeetingCalendarIssue,
} from "@/features/pswg/meeting-request/service/meeting-calendar-service";
import {
    getMeetingRequestFont,
    i18n,
    tText,
    type UiLang,
} from "@/features/pswg/meeting-request/meeting-request-i18n";
import { isDocumentPlaceholder } from "@/lib/document-file";

type MeetingRequestDetailDialogProps = {
    open: boolean;
    event: MeetingCalendarEvent | null;
    language?: UiLang;
    onClose: () => void;
};

type DialogTab = "request" | "update";

function getUploadBaseUrl() {
    const raw =
        process.env.NEXT_PUBLIC_API_URL ??
        process.env.NEXT_PUBLIC_API_BASE_URL ??
        "http://localhost:3001";

    return raw.replace(/\/api\/v1\/?$/, "").replace(/\/+$/, "");
}

function resolveUploadUrl(path?: string | null) {
    if (!path) return null;

    const value = String(path).trim();
    if (!value) return null;
    if (/^https?:\/\//i.test(value)) return value;

    const normalizedPath = value.startsWith("/") ? value : `/${value}`;

    return `${getUploadBaseUrl()}${normalizedPath}`;
}

function AgencyLogo({
    logo,
    name,
    size = 18,
}: {
    logo?: string | null;
    name?: string | null;
    size?: number;
}) {
    const [failed, setFailed] = useState(false);
    const src = !failed ? resolveUploadUrl(logo) : null;

    if (!src) {
        return (
            <DashboardAssetIcon
                src={dashboardAssets.primaryAgencyAvatar}
                width={size}
                height={size}
            />
        );
    }

    return (
        <Box
            component="img"
            src={src}
            alt={name || "Agency logo"}
            onError={() => setFailed(true)}
            sx={{
                width: size,
                height: size,
                borderRadius: "50%",
                objectFit: "cover",
                flexShrink: 0,
                bgcolor: "#ffffff",
                border: "1px solid #e9eaeb",
            }}
        />
    );
}


function LineIcon({ type, size = 18 }: { type: "calendar" | "user" | "status" | "link" | "tag"; size?: number }) {
    const common = {
        width: size,
        height: size,
        viewBox: "0 0 24 24",
        fill: "none",
        stroke: "currentColor",
        strokeWidth: 1.8,
        strokeLinecap: "round" as const,
        strokeLinejoin: "round" as const,
    };

    if (type === "calendar") {
        return (
            <Box component="svg" {...common}>
                <path d="M8 2v4" />
                <path d="M16 2v4" />
                <rect x="3" y="4" width="18" height="18" rx="2" />
                <path d="M3 10h18" />
            </Box>
        );
    }

    if (type === "user") {
        return (
            <Box component="svg" {...common}>
                <path d="M20 21a8 8 0 0 0-16 0" />
                <circle cx="12" cy="7" r="4" />
            </Box>
        );
    }

    if (type === "link") {
        return (
            <Box component="svg" {...common}>
                <path d="M10 13a5 5 0 0 0 7.07 0l2.12-2.12a5 5 0 0 0-7.07-7.07L11 4.93" />
                <path d="M14 11a5 5 0 0 0-7.07 0L4.81 13.12a5 5 0 0 0 7.07 7.07L13 19.07" />
            </Box>
        );
    }

    if (type === "tag") {
        return (
            <Box component="svg" {...common}>
                <path d="M20.59 13.41 12 22l-9-9V4h9l8.59 8.59a2 2 0 0 1 0 2.82Z" />
                <circle cx="7.5" cy="8.5" r="1" />
            </Box>
        );
    }

    return (
        <Box component="svg" {...common}>
            <circle cx="12" cy="12" r="10" />
            <path d="M12 8v4" />
            <path d="M12 16h.01" />
        </Box>
    );
}

function getStatusStyle(status: MeetingCalendarEvent["status"]) {
    if (status === "Completed") {
        return {
            color: "#1a64a8",
            bg: "#eff8ff",
            border: "#b2ddff",
        };
    }

    if (status === "Submitted") {
        return {
            color: "#12b76a",
            bg: "#ecfdf3",
            border: "#d1fadf",
        };
    }

    if (status === "Under Review" || status === "Draft") {
        return {
            color: status === "Draft" ? "#f04438" : "#f79009",
            bg: status === "Draft" ? "#fef3f2" : "#fffaeb",
            border: status === "Draft" ? "#fee4e2" : "#fedf89",
        };
    }

    if (status === "Scheduled") {
        return {
            color: "#667085",
            bg: "#f9fafb",
            border: "#eaecf0",
        };
    }

    return {
        color: "#717680",
        bg: "#f5f5f5",
        border: "#e9eaeb",
    };
}

function DetailItem({
    icon,
    label,
    value,
    children,
}: {
    icon?: ReactNode;
    label: string;
    value?: string;
    children?: ReactNode;
}) {
    const theme = useTheme();
    const isDark = theme.palette.mode === "dark";

    return (
        <Box
            sx={{
                display: "flex",
                alignItems: "center",
                gap: 1,
                minWidth: 0,
                maxWidth: "100%",
                overflow: "hidden",
            }}
        >
            <Box
                sx={{
                    width: 18,
                    height: 18,
                    display: "grid",
                    placeItems: "center",
                    color: isDark ? alpha("#ffffff", 0.7) : "#717680",
                    flexShrink: 0,
                }}
            >
                {icon}
            </Box>

            <Typography
                sx={{
                    color: isDark ? alpha("#ffffff", 0.65) : "#717680",
                    fontSize: 12,
                    fontWeight: 600,
                    whiteSpace: "nowrap",
                    flexShrink: 0,
                }}
            >
                {label} :
            </Typography>

            <Box sx={{ minWidth: 0, maxWidth: "100%", overflow: "hidden" }}>
                {children ?? (
                    <Typography
                        noWrap
                        sx={{
                            color: isDark ? "#ffffff" : "#252b37",
                            fontSize: 12,
                            fontWeight: 700,
                            minWidth: 0,
                        }}
                    >
                        {value}
                    </Typography>
                )}
            </Box>
        </Box>
    );
}

function TabButton({
    active,
    label,
    language,
    onClick,
}: {
    active: boolean;
    label: string;
    language: UiLang;
    onClick: () => void;
}) {
    return (
        <ButtonBase
            onClick={onClick}
            sx={{
                height: 38,
                px: 0,
                alignItems: "flex-end",
                borderBottom: active ? "2px solid #1a64a8" : "2px solid transparent",
            }}
        >
            <Typography
                sx={{
                    pb: 1.15,
                    color: active ? "#1a64a8" : "#717680",
                    fontSize: 12,
                    fontWeight: 600,
                    fontFamily: getMeetingRequestFont(language),
                }}
            >
                {label}
            </Typography>
        </ButtonBase>
    );
}

function DocumentCard({ path }: { path?: string | null }) {
    if (isDocumentPlaceholder(path) || !path) {
        return (
            <Typography sx={{ fontSize: 12, fontWeight: 500, color: "#252b37" }}>
                -
            </Typography>
        );
    }

    return (
        <DocumentLink
            file={{ path }}
            sx={{
                display: "inline-flex",
                alignItems: "center",
                minWidth: 0,
                maxWidth: 210,
            }}
        >
            <DocumentFileAttachment
                value={path}
                iconSize={20}
                emptyLabel="-"
            />
        </DocumentLink>
    );
}

function IssueDetailDialog({
    open,
    issue,
    event,
    language,
    onClose,
}: {
    open: boolean;
    issue: MeetingCalendarIssue | null;
    event: MeetingCalendarEvent;
    language: UiLang;
    onClose: () => void;
}) {
    const theme = useTheme();
    const isDark = theme.palette.mode === "dark";
    const statusStyle = getStatusStyle(event.status);

    if (!issue) return null;

    return (
        <>
            <Dialog
                open={open}
                onClose={onClose}
                maxWidth={false}
                slotProps={{
                    backdrop: {
                        sx: { backgroundColor: alpha("#000000", 0.55) },
                    },
                    paper: {
                        sx: {
                            width: 760,
                            maxWidth: "calc(100vw - 32px)",
                            maxHeight: "calc(100vh - 64px)",
                            borderRadius: "8px",
                            backgroundColor: isDark ? "#101828" : "#ffffff",
                            border: isDark ? `1px solid ${alpha("#ffffff", 0.12)}` : "none",
                            boxShadow: isDark
                                ? "0px 24px 70px rgba(0, 0, 0, 0.55)"
                                : "0px 20px 50px rgba(0, 0, 0, 0.25)",
                            overflow: "hidden",
                        },
                    },
                }}
            >
                <DialogContent
                    sx={{
                        p: 0,
                        overflowY: "auto",
                        overflowX: "hidden",
                        maxWidth: "100%",
                        boxSizing: "border-box",
                        "&::-webkit-scrollbar": { width: 6 },
                        "&::-webkit-scrollbar-thumb": {
                            borderRadius: 999,
                            backgroundColor: isDark
                                ? alpha("#ffffff", 0.18)
                                : alpha("#000000", 0.16),
                        },
                    }}
                >
                    <Box
                        sx={{
                            p: 3,
                            width: "100%",
                            maxWidth: "100%",
                            minWidth: 0,
                            boxSizing: "border-box",
                            overflowX: "hidden",
                        }}
                    >
                        <Box
                            sx={{
                                display: "flex",
                                alignItems: "flex-start",
                                justifyContent: "space-between",
                                gap: 2,
                            }}
                        >
                            <Typography
                                sx={{
                                    color: isDark ? "#ffffff" : "#181d27",
                                    fontSize: 20,
                                    fontWeight: 700,
                                    fontFamily: getMeetingRequestFont(language),
                                    wordBreak: "break-word",
                                }}
                            >
                                {tText(issue.title, language)}
                            </Typography>

                            <IconButton
                                onClick={onClose}
                                sx={{
                                    width: 32,
                                    height: 32,
                                    color: isDark ? alpha("#ffffff", 0.72) : "#717680",
                                }}
                            >
                                ×
                            </IconButton>
                        </Box>

                        <Box
                            sx={{
                                mt: 3,
                                display: "grid",
                                gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
                                gap: 2,
                            }}
                        >
                            <DetailItem label="Government Agency" icon={<LineIcon type="user" />}>
                                <Box
                                    sx={{
                                        display: "flex",
                                        alignItems: "center",
                                        gap: 0.75,
                                        minWidth: 0,
                                        overflow: "hidden",
                                    }}
                                >
                                    <AgencyLogo
                                        logo={issue.governmentAgencyLogo || event.governmentAgencyLogo}
                                        name={issue.governmentAgency || event.governmentAgency}
                                        size={18}
                                    />

                                    <Typography
                                        noWrap
                                        sx={{
                                            color: isDark ? "#ffffff" : "#252b37",
                                            fontSize: 12,
                                            fontWeight: 700,
                                        }}
                                    >
                                        {issue.governmentAgency || event.governmentAgency}
                                    </Typography>
                                </Box>
                            </DetailItem>
                            <DetailItem
                                icon={<LineIcon type="calendar" />}
                                label="Submitted Date"
                                value={formatCalendarDate(
                                    issue.submittedDate || event.submittedDate,
                                    language,
                                )}
                            />
                            <DetailItem label="Category" value={issue.category || "—"} icon={<LineIcon type="tag" />} />
                            <DetailItem label="Status" icon={<LineIcon type="status" />}>
                                <Box
                                    sx={{
                                        height: 22,
                                        px: 1,
                                        borderRadius: "999px",
                                        backgroundColor: statusStyle.bg,
                                        border: `1px solid ${statusStyle.border}`,
                                        display: "inline-flex",
                                        alignItems: "center",
                                    }}
                                >
                                    <Typography
                                        sx={{
                                            color: statusStyle.color,
                                            fontSize: 11,
                                            fontWeight: 700,
                                        }}
                                    >
                                        {issue.status || event.status}
                                    </Typography>
                                </Box>
                            </DetailItem>
                        </Box>

                        <Typography
                            sx={{
                                mt: 3,
                                color: isDark ? "#ffffff" : "#181d27",
                                fontSize: 18,
                                fontWeight: 700,
                                fontFamily: getMeetingRequestFont(language),
                            }}
                        >
                            Issue Description
                        </Typography>
                        <Box
                            sx={{
                                mt: 1.25,
                                p: 2,
                                borderRadius: "8px",
                                backgroundColor: isDark ? alpha("#ffffff", 0.06) : "#fafafa",
                            }}
                        >
                            <Typography
                                sx={{
                                    color: isDark ? alpha("#ffffff", 0.82) : "#535862",
                                    fontSize: 14,
                                    lineHeight: 1.9,
                                    whiteSpace: "pre-line",
                                    wordBreak: "break-word",
                                    fontFamily: getMeetingRequestFont(language),
                                }}
                            >
                                {tText(issue.description || "—", language)}
                            </Typography>
                        </Box>

                        <Typography
                            sx={{
                                mt: 3,
                                color: isDark ? "#ffffff" : "#181d27",
                                fontSize: 18,
                                fontWeight: 700,
                                fontFamily: getMeetingRequestFont(language),
                            }}
                        >
                            Recommendation
                        </Typography>
                        <Box
                            sx={{
                                mt: 1.25,
                                p: 2,
                                borderRadius: "8px",
                                backgroundColor: isDark ? alpha("#ffffff", 0.06) : "#fafafa",
                            }}
                        >
                            <Typography
                                sx={{
                                    color: isDark ? alpha("#ffffff", 0.82) : "#535862",
                                    fontSize: 14,
                                    lineHeight: 1.9,
                                    whiteSpace: "pre-line",
                                    wordBreak: "break-word",
                                    fontFamily: getMeetingRequestFont(language),
                                }}
                            >
                                {tText(issue.recommendation || "—", language)}
                            </Typography>
                        </Box>
                    </Box>
                </DialogContent>
            </Dialog>
        </>
    );
}

export function MeetingRequestDetailDialog({
    open,
    event,
    language = "km",
    onClose,
}: MeetingRequestDetailDialogProps) {
    const theme = useTheme();
    const isDark = theme.palette.mode === "dark";
    const t = i18n(language);
    const [activeTab, setActiveTab] = useState<DialogTab>("request");
    const [selectedIssue, setSelectedIssue] =
        useState<MeetingCalendarIssue | null>(null);

    if (!event) return null;

    const statusStyle = getStatusStyle(event.status);

    return (
        <>
            <Dialog
                open={open && !selectedIssue}
                onClose={onClose}
                maxWidth={false}
                slotProps={{
                    backdrop: {
                        sx: {
                            backgroundColor: alpha("#000000", 0.55),
                        },
                    },
                    paper: {
                        sx: {
                            width: 760,
                            maxWidth: "calc(100vw - 32px)",
                            maxHeight: "calc(100vh - 64px)",
                            borderRadius: "8px",
                            backgroundColor: isDark ? "#101828" : "#ffffff",
                            border: isDark ? `1px solid ${alpha("#ffffff", 0.12)}` : "none",
                            boxShadow: isDark
                                ? "0px 24px 70px rgba(0, 0, 0, 0.55)"
                                : "0px 20px 50px rgba(0, 0, 0, 0.25)",
                            overflow: "hidden",
                        },
                    },
                }}
            >
                <DialogContent
                    sx={{
                        p: 0,
                        overflowY: "auto",
                        overflowX: "hidden",
                        maxWidth: "100%",
                        boxSizing: "border-box",
                        "&::-webkit-scrollbar": {
                            width: 6,
                            height: 0,
                        },
                        "&::-webkit-scrollbar-thumb": {
                            borderRadius: 999,
                            backgroundColor: isDark
                                ? alpha("#ffffff", 0.18)
                                : alpha("#000000", 0.16),
                        },
                    }}
                >
                    <Box
                        sx={{
                            p: 3,
                            width: "100%",
                            maxWidth: "100%",
                            minWidth: 0,
                            boxSizing: "border-box",
                            overflowX: "hidden",
                        }}
                    >
                        <Box
                            sx={{
                                display: "flex",
                                alignItems: "flex-start",
                                justifyContent: "space-between",
                                gap: 2,
                                minWidth: 0,
                            }}
                        >
                            <Typography
                                sx={{
                                    color: isDark ? "#ffffff" : "#181d27",
                                    fontSize: 20,
                                    fontWeight: 700,
                                    lineHeight: 1.5,
                                    fontFamily: getMeetingRequestFont(language),
                                    minWidth: 0,
                                }}
                            >
                                {tText(event.title, language)}
                            </Typography>

                            <IconButton
                                onClick={onClose}
                                sx={{
                                    width: 32,
                                    height: 32,
                                    color: isDark ? alpha("#ffffff", 0.72) : "#717680",
                                    flexShrink: 0,
                                }}
                            >
                                ×
                            </IconButton>
                        </Box>

                        <Box
                            sx={{
                                mt: 2,
                                display: "flex",
                                alignItems: "flex-end",
                                gap: 4,
                                borderBottom: `1px solid ${isDark ? alpha("#ffffff", 0.12) : "#e9eaeb"
                                    }`,
                            }}
                        >
                            <TabButton
                                active={activeTab === "request"}
                                label={t.requestDetails}
                                language={language}
                                onClick={() => setActiveTab("request")}
                            />

                            {event.meetingUpdate && (
                                <TabButton
                                    active={activeTab === "update"}
                                    label={t.meetingUpdate}
                                    language={language}
                                    onClick={() => setActiveTab("update")}
                                />
                            )}
                        </Box>

                        {activeTab === "update" && event.meetingUpdate ? (
                            <Box sx={{ mt: 2.25 }}>
                                <Box
                                    sx={{
                                        display: "grid",
                                        gridTemplateColumns: { xs: "1fr", sm: "1fr 1fr" },
                                        gap: 2,
                                        minWidth: 0,
                                    }}
                                >
                                    <DetailItem
                                        label={t.meetingDate}
                                        value={`${formatCalendarDate(
                                            event.meetingUpdate.meetingDate,
                                            language,
                                        )} | ${formatCalendarTimeRange(
                                            event.meetingUpdate.startTime,
                                            event.meetingUpdate.endTime,
                                            language,
                                        )}`}
                                        icon={<LineIcon type="calendar" />}
                                    />

                                    <DetailItem label={t.meetingReferenceDocument} icon={<LineIcon type="link" />}>
                                        <DocumentCard
                                            path={event.meetingUpdate.referenceDocumentName}
                                        />
                                    </DetailItem>
                                </Box>

                                <Typography
                                    sx={{
                                        mt: 3,
                                        color: isDark ? "#ffffff" : "#181d27",
                                        fontSize: 18,
                                        fontWeight: 700,
                                        fontFamily: getMeetingRequestFont(language),
                                    }}
                                >
                                    {tText(event.meetingUpdate.progressSolutionTitle, language)}
                                </Typography>

                                <Box
                                    sx={{
                                        mt: 1.25,
                                        p: 2,
                                        borderRadius: "8px",
                                        backgroundColor: isDark
                                            ? alpha("#ffffff", 0.06)
                                            : "#fafafa",
                                        overflow: "hidden",
                                    }}
                                >
                                    <Typography
                                        sx={{
                                            color: isDark ? alpha("#ffffff", 0.82) : "#535862",
                                            fontSize: 14,
                                            fontWeight: 400,
                                            lineHeight: 1.9,
                                            fontFamily: getMeetingRequestFont(language),
                                            whiteSpace: "pre-line",
                                            wordBreak: "break-word",
                                        }}
                                    >
                                        {tText(
                                            event.meetingUpdate.progressSolutionDescription,
                                            language,
                                        )}
                                    </Typography>
                                </Box>

                                <Typography
                                    sx={{
                                        mt: 3,
                                        color: isDark ? "#ffffff" : "#181d27",
                                        fontSize: 18,
                                        fontWeight: 700,
                                        fontFamily: getMeetingRequestFont(language),
                                    }}
                                >
                                    {tText(event.meetingUpdate.solutionIndicatorsTitle, language)}
                                </Typography>

                                <Box
                                    sx={{
                                        mt: 1.25,
                                        p: 2,
                                        borderRadius: "8px",
                                        backgroundColor: isDark
                                            ? alpha("#ffffff", 0.06)
                                            : "#fafafa",
                                        overflow: "hidden",
                                    }}
                                >
                                    <Typography
                                        sx={{
                                            color: isDark ? alpha("#ffffff", 0.82) : "#535862",
                                            fontSize: 14,
                                            fontWeight: 400,
                                            lineHeight: 1.9,
                                            fontFamily: getMeetingRequestFont(language),
                                            whiteSpace: "pre-line",
                                            wordBreak: "break-word",
                                        }}
                                    >
                                        {tText(
                                            event.meetingUpdate.solutionIndicatorsDescription,
                                            language,
                                        )}
                                    </Typography>
                                </Box>
                            </Box>
                        ) : (
                            <Box sx={{ mt: 2.25 }}>
                                <Box
                                    sx={{
                                        display: "grid",
                                        gridTemplateColumns: { xs: "1fr", sm: "1fr 1.15fr" },
                                        columnGap: 4,
                                        rowGap: 1.75,
                                        minWidth: 0,
                                        overflowX: "hidden",
                                    }}
                                >
                                    <DetailItem
                                        label={t.submittedDate}
                                        value={formatCalendarDate(
                                            event.submittedDate,
                                            language,
                                        )}
                                        icon={<LineIcon type="calendar" />}
                                    />

                                    <DetailItem label={t.governmentAgency} icon={<LineIcon type="user" />}>
                                        <Box
                                            sx={{
                                                display: "flex",
                                                alignItems: "center",
                                                gap: 0.75,
                                                minWidth: 0,
                                                overflow: "hidden",
                                            }}
                                        >
                                            <AgencyLogo
                                                logo={event.governmentAgencyLogo}
                                                name={event.governmentAgency}
                                                size={18}
                                            />

                                            <Typography
                                                noWrap
                                                sx={{
                                                    color: isDark ? "#ffffff" : "#252b37",
                                                    fontSize: 12,
                                                    fontWeight: 700,
                                                }}
                                            >
                                                {event.governmentAgency}
                                            </Typography>
                                        </Box>
                                    </DetailItem>

                                    <DetailItem label={t.status} icon={<LineIcon type="status" />}>
                                        <Box
                                            sx={{
                                                height: 22,
                                                px: 1,
                                                borderRadius: "999px",
                                                backgroundColor: statusStyle.bg,
                                                border: `1px solid ${statusStyle.border}`,
                                                display: "inline-flex",
                                                alignItems: "center",
                                                justifyContent: "center",
                                            }}
                                        >
                                            <Typography
                                                sx={{
                                                    color: statusStyle.color,
                                                    fontSize: 11,
                                                    fontWeight: 700,
                                                    fontFamily: getMeetingRequestFont(language),
                                                }}
                                            >
                                                {t.statuses[
                                                    event.status === "Draft"
                                                        ? "Drafted"
                                                        : event.status
                                                ] ?? event.status}
                                            </Typography>
                                        </Box>
                                    </DetailItem>

                                    <DetailItem label={t.meetingRequestDocument} icon={<LineIcon type="link" />}>
                                        <DocumentCard path={event.documentName} />
                                    </DetailItem>
                                </Box>

                                <Typography
                                    sx={{
                                        mt: 3,
                                        color: isDark ? "#ffffff" : "#181d27",
                                        fontSize: 18,
                                        fontWeight: 700,
                                        fontFamily: getMeetingRequestFont(language),
                                    }}
                                >
                                    {t.description}
                                </Typography>

                                <Box
                                    sx={{
                                        mt: 1.25,
                                        p: 2,
                                        borderRadius: "8px",
                                        backgroundColor: isDark
                                            ? alpha("#ffffff", 0.06)
                                            : "#fafafa",
                                        maxWidth: "100%",
                                        overflowX: "hidden",
                                    }}
                                >
                                    <Typography
                                        sx={{
                                            color: isDark ? alpha("#ffffff", 0.82) : "#535862",
                                            fontSize: 14,
                                            fontWeight: 400,
                                            lineHeight: 1.9,
                                            fontFamily: getMeetingRequestFont(language),
                                            whiteSpace: "pre-line",
                                            wordBreak: "break-word",
                                        }}
                                    >
                                        {tText(event.description, language)}
                                    </Typography>
                                </Box>

                                <Box
                                    sx={{
                                        mt: 3,
                                        display: "flex",
                                        alignItems: "center",
                                        gap: 1,
                                    }}
                                >
                                    <Typography
                                        sx={{
                                            color: isDark ? "#ffffff" : "#181d27",
                                            fontSize: 18,
                                            fontWeight: 700,
                                            fontFamily: getMeetingRequestFont(language),
                                        }}
                                    >
                                        {t.issueSubmitted}
                                    </Typography>

                                    <Box
                                        sx={{
                                            minWidth: 24,
                                            height: 22,
                                            px: 0.75,
                                            borderRadius: "4px",
                                            border: `1px solid ${isDark ? alpha("#f04438", 0.32) : "#fee4e2"
                                                }`,
                                            display: "grid",
                                            placeItems: "center",
                                        }}
                                    >
                                        <Typography
                                            sx={{ color: "#f04438", fontSize: 12, fontWeight: 700 }}
                                        >
                                            {event.issues.length}
                                        </Typography>
                                    </Box>
                                </Box>

                                <Box
                                    sx={{
                                        mt: 1.25,
                                        display: "flex",
                                        flexDirection: "column",
                                        gap: 1,
                                    }}
                                >
                                    {event.issues.map((issue, index) => (
                                        <ButtonBase
                                            key={`${event.id}-issue-${index}`}
                                            type="button"
                                            onClick={() => setSelectedIssue(issue)}
                                            sx={{
                                                width: "100%",
                                                p: 2,
                                                display: "block",
                                                textAlign: "left",
                                                borderRadius: "8px",
                                                border: `1px solid ${isDark ? alpha("#ffffff", 0.1) : "#e9eaeb"
                                                    }`,
                                                backgroundColor: isDark
                                                    ? alpha("#ffffff", 0.04)
                                                    : "#ffffff",
                                                minWidth: 0,
                                                overflow: "hidden",
                                                transition: "all 0.2s ease",
                                                "&:hover": {
                                                    borderColor: "#1a64a8",
                                                    backgroundColor: isDark
                                                        ? alpha("#1a64a8", 0.16)
                                                        : "#f7fbff",
                                                },
                                            }}
                                        >
                                            <Typography
                                                sx={{
                                                    color: isDark ? "#ffffff" : "#252b37",
                                                    fontSize: 14,
                                                    fontWeight: 700,
                                                    fontFamily: getMeetingRequestFont(language),
                                                }}
                                            >
                                                {tText(issue.title, language)}
                                            </Typography>

                                            <Typography
                                                sx={{
                                                    mt: 0.75,
                                                    color: isDark ? alpha("#ffffff", 0.65) : "#717680",
                                                    fontSize: 13,
                                                    fontWeight: 400,
                                                    lineHeight: 1.6,
                                                    wordBreak: "break-word",
                                                    fontFamily: getMeetingRequestFont(language),
                                                }}
                                            >
                                                {tText(issue.description, language)}
                                            </Typography>
                                        </ButtonBase>
                                    ))}
                                </Box>
                            </Box>
                        )}
                    </Box>
                </DialogContent>
            </Dialog>

            <IssueDetailDialog
                open={Boolean(selectedIssue)}
                issue={selectedIssue}
                event={event}
                language={language}
                onClose={() => setSelectedIssue(null)}
            />
        </>
    );
}
