import { useLocation, Navigate } from 'react-router-dom';
import type { ResumeAnalysisResult } from '../types/resumeAnalysis';
import {
  AnalysisHeader,
  ScoreCard,
  CriteriaSection,
  FeedbackSection,
  GrammarSection,
  InjectionWarning,
  AnalysisActions,
} from '../components/analysis';
import '../assets/css/demo.css';

/**
 * Results page component displaying analysis results
 * Trace: SDD_LLD_0023 - Display numeric score output
 * Trace: SDD_LLD_0024 - Display criteria breakdown results
 * Trace: SDD_LLD_0025 - Display summary, strengths, weaknesses
 * Trace: SDD_LLD_0026 - Display grammar rating
 * Trace: SDD_HLD_0014 - Display results of output to user through UI
 * Trace: SDD_LLD_0028 - Provide navigation across UI pages (redirect when no data)
 */
export default function ResultsPage() {
  const location = useLocation();

  // Redirect to home page if user navigates directly without analysis data
  // Trace: SDD_LLD_0028 - Provide navigation across UI pages
  if (!location.state) {
    return <Navigate to="/" replace />;
  }

  const analysisData: ResumeAnalysisResult = location.state as ResumeAnalysisResult;

  return (
    <div className="demo-page">
      <div className="demo-container">
        <AnalysisHeader
          title={
            <>
              Resume <span className="gradient-text">Analysis Results</span>
            </>
          }
          subtitle="Your resume has been analyzed against the job requirements"
        />

        {/* Trace: SRD_NonFuncReq_0010 - Display prompt injection warning if detected */}
        {/* Trace: SRD_QualAssurReq_0005 - Alert user of neutralized injection attempts */}
        <InjectionWarning
          injectionDetected={analysisData.injection_detected}
          injectionDetails={analysisData.injection_details}
        />

        {/* Trace: SDD_LLD_0023 - Renders final percentage score and criteria ratings */}
        {/* Trace: SDD_HLD_0010 - Generate graded subsection categories on scale from 1-10 */}
        <ScoreCard data={analysisData} />

        {/* Trace: SDD_LLD_0024 - Display criteria breakdown results */}
        {/* Trace: SDD_HLD_0009 - Decompose output into job description criteria-based subsections */}
        <CriteriaSection criteria={analysisData.criteria_scores} />

        {/* Trace: SDD_LLD_0025 - Display summary, strengths, weaknesses */}
        {/* Trace: SDD_HLD_0011 - Generate strengths and weaknesses found in resumes */}
        <FeedbackSection
          strengths={analysisData.strengths}
          weaknesses={analysisData.weaknesses}
          missingInformation={analysisData.missing_information}
        />

        {/* Trace: SDD_LLD_0026 - Display grammar rating */}
        {/* Trace: SRD_FuncReq_0010 - Display grammar/spelling score */}
        {/* Trace: SRD_FuncReq_0011 - Display grammar corrections */}
        {/* Trace: SRD_InterfaceReq_0010 - Display rating of grammar and spelling errors */}
        <GrammarSection grammarSpelling={analysisData.grammar_spelling} />

        {/* Trace: SRD_FuncReq_0018 - Allow user to download response as JSON file */}
        <AnalysisActions analysisData={analysisData} />
      </div>
    </div>
  );
}
