import React from "react";
import { Alert, Box, Button, Chip, CircularProgress, Typography } from "@mui/material";

import { useI18n } from "../i18n/I18nProvider";
import { CandidateFit, FocusPlanResponse, InterviewPrepResponse, MatchScore, ReadinessResponse, UserOperation } from "../types";
import { DraftCard, ListCard, MatchScoreCard, SectionChips, TwoColumnSection } from "./JobDetailsPanels";

type Props = {
  tab: number;
  matchScore: MatchScore | null;
  loadingMatchScore: boolean;
  updateLearningRecommendation: (id: number, status: "done" | "dismissed") => void;
  candidateFit: CandidateFit | null;
  loadingCandidateFit: boolean;
  regenerateCandidateFit: () => void;
  fitLevel: { label: string; color: "success" | "warning" | "default" } | null;
  focusPlan: FocusPlanResponse | null;
  loadingFocusPlan: boolean;
  regenerateFocusPlan: () => void;
  focusPlanOperation: UserOperation | null;
  cancelFocusPlan: () => void;
  retryFocusPlan: () => void;
  interviewPrep: InterviewPrepResponse | null;
  loadingInterviewPrep: boolean;
  regenerateInterviewPrep: () => void;
  readiness: ReadinessResponse | null;
  loadingReadiness: boolean;
};

export default function JobInsightTabs(props: Props) {
  const { t } = useI18n();
  const { tab, matchScore, loadingMatchScore, updateLearningRecommendation, candidateFit, loadingCandidateFit, regenerateCandidateFit, fitLevel, focusPlan, loadingFocusPlan, regenerateFocusPlan, focusPlanOperation, cancelFocusPlan, retryFocusPlan, interviewPrep, loadingInterviewPrep, regenerateInterviewPrep, readiness, loadingReadiness } = props;
  return <>
        {tab === 5 && (
          <Box>
            <MatchScoreCard score={matchScore} loading={loadingMatchScore} onUpdateLearningRecommendation={updateLearningRecommendation} />
            <Box sx={{ display: "flex", justifyContent: "flex-end", my: 1.5 }}>
              <Button size="small" variant="outlined" disabled={loadingCandidateFit} onClick={regenerateCandidateFit}>
                {loadingCandidateFit ? "Regenerating..." : "Regenerate"}
              </Button>
            </Box>
            {loadingCandidateFit ? <Box sx={{ py: 4, display: "flex", justifyContent: "center" }}><CircularProgress size={28} /></Box> : candidateFit ? (
              <Box sx={{ display: "flex", flexDirection: "column", gap: 2.5 }}>
                <Typography variant="caption" sx={{ color: "text.secondary", display: "block", mt: -1 }}>{t("jobDetailsAiFitHint")}</Typography>
                <Box sx={{ display: "flex", justifyContent: "space-between", alignItems: "center", gap: 1, flexWrap: "wrap" }}>
                  <Box><Typography variant="overline">{t("jobDetailsHowYouMatch")}</Typography><Typography sx={{ whiteSpace: "pre-wrap" }}>{candidateFit.matchSummary}</Typography></Box>
                  <Box sx={{ display: "flex", gap: 1, flexWrap: "wrap", alignItems: "center" }}>
                    <Chip label={t("jobDetailsMatchPercent", { count: candidateFit.matchScore })} color={candidateFit.matchScore >= 75 ? "success" : candidateFit.matchScore >= 55 ? "warning" : "default"} size="small" />
                    {fitLevel ? <Chip label={fitLevel.label} color={fitLevel.color} size="small" /> : null}
                  </Box>
                </Box>
                <DraftCard title={t("jobDetailsTailoredPitch")} content={candidateFit.tailoredPitch} />
                <SectionChips title={t("jobDetailsStrongMatches")} items={candidateFit.strengths} color="success" />
                <SectionChips title={t("jobDetailsPossibleGaps")} items={candidateFit.gaps} color="warning" outlined />
                <TwoColumnSection leftTitle={t("jobDetailsWhatToMention")} leftItems={candidateFit.mention} rightTitle={t("jobDetailsWhatNotToOverstate")} rightItems={candidateFit.avoid} />
                <TwoColumnSection leftTitle={t("jobDetailsImproveCv")} leftItems={candidateFit.cvImprovements} rightTitle={t("jobDetailsMissingKeywords")} rightItems={candidateFit.missingKeywords} />
                <TwoColumnSection leftTitle={t("jobDetailsTabInterviewPrep")} leftItems={candidateFit.interviewPrep} rightTitle={t("jobDetailsCvGuidance")} rightItems={candidateFit.guidance.cv} />
                <TwoColumnSection leftTitle={t("jobDetailsCoverLetterGuidance")} leftItems={candidateFit.guidance.coverLetter} rightTitle={t("jobDetailsRecruiterMessageGuidance")} rightItems={candidateFit.guidance.recruiterMessage} />
                <Box sx={{ display: "grid", gridTemplateColumns: { xs: "1fr", md: "1fr 1fr" }, gap: 2 }}>
                  <DraftCard title={t("jobDetailsCoverLetterDraft")} content={candidateFit.coverLetterDraft ?? t("jobDetailsNoDraftAvailableYet")} />
                  <DraftCard title={t("jobDetailsRecruiterMessageDraft")} content={candidateFit.recruiterMessageDraft ?? t("jobDetailsNoDraftAvailableYet")} />
                </Box>
              </Box>
            ) : <Typography sx={{ color: "text.secondary" }}>{t("jobDetailsCandidateFitEmpty")}</Typography>}
          </Box>
        )}

        {tab === 6 && (
          <Box>
            <Box sx={{ display: "flex", justifyContent: "flex-end", mb: 1.5 }}>
              <Button size="small" variant="outlined" disabled={loadingFocusPlan} onClick={regenerateFocusPlan}>
                {loadingFocusPlan ? "Starting..." : focusPlan ? "Regenerate" : "Generate"}
              </Button>
            </Box>
            {focusPlanOperation && focusPlanOperation.status !== "succeeded" ? (
              <Alert severity={focusPlanOperation.status === "failed" ? "error" : focusPlanOperation.status === "cancelled" ? "warning" : "info"} sx={{ mb: 2 }}
                action={<>
                  {focusPlanOperation.canCancel ? <Button size="small" color="inherit" onClick={cancelFocusPlan}>Cancel</Button> : null}
                  {focusPlanOperation.canRetry ? <Button size="small" color="inherit" onClick={retryFocusPlan}>Retry</Button> : null}
                </>}>
                Strategy snapshot: {operationLabel(focusPlanOperation)}
              </Alert>
            ) : null}
            {loadingFocusPlan && !focusPlan ? <Box sx={{ py: 4, display: "flex", justifyContent: "center" }}><CircularProgress size={28} /></Box> : focusPlan ? (
              <Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
                <DraftCard title={t("jobDetailsFocusSummary")} content={focusPlan.strategicSummary} />
                <TwoColumnSection leftTitle={t("jobDetailsImmediatePriorities")} leftItems={focusPlan.immediatePriorities} rightTitle={t("jobDetailsProofPoints")} rightItems={focusPlan.proofPointsToLeadWith} />
                <TwoColumnSection leftTitle={t("jobDetailsCvBulletIdeas")} leftItems={focusPlan.cvBulletIdeas} rightTitle={t("jobDetailsCoverLetterAngles")} rightItems={focusPlan.coverLetterAngles} />
                <ListCard title={t("jobDetailsFollowUpApproach")} items={focusPlan.followUpApproach} />
              </Box>
            ) : <Typography sx={{ color: "text.secondary" }}>{t("jobDetailsNoFocusPlan")}</Typography>}
          </Box>
        )}

        {tab === 7 && (
          <Box>
            <Box sx={{ display: "flex", justifyContent: "flex-end", mb: 1.5 }}>
              <Button size="small" variant="outlined" disabled={loadingInterviewPrep} onClick={regenerateInterviewPrep}>
                {loadingInterviewPrep ? "Regenerating..." : "Regenerate"}
              </Button>
            </Box>
            {loadingInterviewPrep ? <Box sx={{ py: 4, display: "flex", justifyContent: "center" }}><CircularProgress size={28} /></Box> : interviewPrep ? (
              <Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
                <DraftCard title={t("jobDetailsInterviewPrepBrief")} content={interviewPrep.summary} />
                <TwoColumnSection leftTitle={t("jobDetailsTalkingPoints")} leftItems={interviewPrep.talkingPoints} rightTitle={t("jobDetailsLikelyQuestions")} rightItems={interviewPrep.likelyQuestions} />
                <ListCard title={t("jobDetailsWeakSpots")} items={interviewPrep.weakSpots} />
              </Box>
            ) : <Typography sx={{ color: "text.secondary" }}>{t("jobDetailsNoInterviewPrep")}</Typography>}
          </Box>
        )}

        {tab === 8 && (
          <Box>
            {loadingReadiness ? <Box sx={{ py: 4, display: "flex", justifyContent: "center" }}><CircularProgress size={28} /></Box> : readiness ? (
              <Box sx={{ display: "flex", flexDirection: "column", gap: 2 }}>
                <Box sx={{ display: "flex", justifyContent: "space-between", alignItems: "center", gap: 1, flexWrap: "wrap" }}>
                  <Typography variant="h6">{t("jobDetailsApplicationReadiness")}</Typography>
                  <Box sx={{ display: "flex", gap: 1, flexWrap: "wrap" }}>
                    <Chip label={t("jobDetailsReadyPercent", { count: readiness.score })} color={readiness.score >= 80 ? "success" : readiness.score >= 60 ? "warning" : "default"} />
                    <Chip label={readiness.level} variant="outlined" />
                  </Box>
                </Box>
                <TwoColumnSection leftTitle={t("jobDetailsCompleted")} leftItems={readiness.completed} rightTitle={t("jobDetailsStillMissing")} rightItems={readiness.missing} />
                <ListCard title={t("jobDetailsSmartReminders")} items={readiness.reminders} />
              </Box>
            ) : <Typography sx={{ color: "text.secondary" }}>{t("jobDetailsNoReadiness")}</Typography>}
          </Box>
        )}
  </>;
}

function operationLabel(operation: UserOperation) {
  if (operation.cancellationRequestedAtUtc) return "cancellation requested";
  switch (operation.status) {
    case "queued": return "queued";
    case "running": return "processing locally";
    case "waiting_for_retry": return "waiting to retry";
    case "waiting_for_external_fallback": return "waiting for approved fallback";
    case "failed": return `failed${operation.failureCategory ? ` (${operation.failureCategory.replaceAll("_", " ")})` : ""}`;
    case "cancelled": return "cancelled";
    default: return "completed";
  }
}
