// =========================================================
// FILE: post-match-report.js | TYPE: Elite Post-Match Report
// =========================================================

import { db } from "./firebase-config.js";
import { requireAuth, setupLogoutButton } from "./app-guard.js";
import { showLoader, hideLoader } from "./loader.js";
import { showToast } from "./toast.js";

import {
  doc,
  getDoc,
  collection,
  getDocs,
  addDoc,
  query,
  where,
  updateDoc,
  deleteDoc,
  serverTimestamp
} from "https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js";

const urlParams = new URLSearchParams(window.location.search);
const matchId = urlParams.get("id");

const reportTitle = document.getElementById("reportTitle");
const reportMeta = document.getElementById("reportMeta");

const reportHomeScore = document.getElementById("reportHomeScore");
const reportAwayScore = document.getElementById("reportAwayScore");
const reportHomeTeam = document.getElementById("reportHomeTeam");
const reportAwayTeam = document.getElementById("reportAwayTeam");
const reportTries = document.getElementById("reportTries");
const reportCards = document.getElementById("reportCards");
const reportAverageRating = document.getElementById("reportAverageRating");
const reportBestRating = document.getElementById("reportBestRating");
const reportBestPlayer = document.getElementById("reportBestPlayer");
const reportResult = document.getElementById("reportResult");
const reportMatchGrade = document.getElementById("reportMatchGrade");
const reportMatchGradeText = document.getElementById("reportMatchGradeText");
const reportStatus = document.getElementById("reportStatus");
const reportStatusText = document.getElementById("reportStatusText");

const reportScrumSuccess = document.getElementById("reportScrumSuccess");
const reportScrumsWon = document.getElementById("reportScrumsWon");
const reportScrumsLost = document.getElementById("reportScrumsLost");

const reportLineoutSuccess = document.getElementById("reportLineoutSuccess");
const reportLineoutsWon = document.getElementById("reportLineoutsWon");
const reportLineoutsLost = document.getElementById("reportLineoutsLost");

const reportRuckTurnoverBalance = document.getElementById("reportRuckTurnoverBalance");
const reportRuckTurnoversFor = document.getElementById("reportRuckTurnoversFor");
const reportRuckTurnoversAgainst = document.getElementById("reportRuckTurnoversAgainst");

const reportTackleCompletion = document.getElementById("reportTackleCompletion");
const reportTacklesMade = document.getElementById("reportTacklesMade");
const reportTacklesMissed = document.getElementById("reportTacklesMissed");

const forwardOfMatchName = document.getElementById("forwardOfMatchName");
const forwardOfMatchPosition = document.getElementById("forwardOfMatchPosition");
const forwardOfMatchRating = document.getElementById("forwardOfMatchRating");

const backOfMatchName = document.getElementById("backOfMatchName");
const backOfMatchPosition = document.getElementById("backOfMatchPosition");
const backOfMatchRating = document.getElementById("backOfMatchRating");

const matchInsightsGrid = document.getElementById("matchInsightsGrid");
const coachSummary = document.getElementById("coachSummary");

const reportStartingSquad = document.getElementById("reportStartingSquad");
const reportBenchSquad = document.getElementById("reportBenchSquad");
const reportTimeline = document.getElementById("reportTimeline");

const printPostMatchBtn = document.getElementById("printPostMatchBtn");
const postMatchNotesForm = document.getElementById("postMatchNotesForm");
const postMatchNotes = document.getElementById("postMatchNotes");
const exportPostMatchCsvBtn = document.getElementById("exportPostMatchCsvBtn");
const saveReportStatusBtn = document.getElementById("saveReportStatusBtn");
const unlockReportBtn = document.getElementById("unlockReportBtn");

const playerRatingForm = document.getElementById("playerRatingForm");
const ratingPlayer = document.getElementById("ratingPlayer");
const ratingScore = document.getElementById("ratingScore");
const ratingComment = document.getElementById("ratingComment");
const playerRatingsList = document.getElementById("playerRatingsList");

let activeProfile = null;
let currentMatch = null;
let currentTeam = null;
let eventsCache = [];
let playersCache = [];
let ratingsCache = [];
let latestSelection = null;
let editingRatingId = null;

const ratingSubmitButton = playerRatingForm?.querySelector("button[type='submit']");

function getPercentage(success, failed) {
  const total = Number(success || 0) + Number(failed || 0);

  if (!total) return "0%";

  return `${Math.round((Number(success || 0) / total) * 100)}%`;
}

function getEventPoints(event) {
  if (event.type === "try") return 5;

  if (event.type === "conversion") {
    return event.conversionResult === "successful" ? 2 : 0;
  }

  if (event.type === "penalty") return 3;
  if (event.type === "drop_goal") return 3;

  return 0;
}

function getPlayerName(player) {
  return `${player?.firstName || ""} ${player?.lastName || ""}`.trim() || "Unnamed Player";
}

function getPlayerTeamIds(player) {
  if (!player) return [];

  const teamIds = Array.isArray(player.teamIds)
    ? [...player.teamIds]
    : [];

  if (player.teamId && !teamIds.includes(player.teamId)) {
    teamIds.push(player.teamId);
  }

  return teamIds.filter(Boolean);
}

function getDateValue(record) {
  if (record?.updatedAt?.toDate) return record.updatedAt.toDate();
  if (record?.createdAt?.toDate) return record.createdAt.toDate();
  if (record?.updatedAt) return new Date(record.updatedAt);
  if (record?.createdAt) return new Date(record.createdAt);
  return new Date(0);
}

function getPlayerNameFromRating(rating) {
  return rating.playerName || "Unknown Player";
}

function canAccessMatch(match) {
  if (!activeProfile || !match) return false;

  const role = activeProfile.role;
  const assignedTeamIds = activeProfile.assignedTeamIds || [];

  if (role === "director") return true;

  if (role === "coach" || role === "biokineticist" || role === "player") {
    return assignedTeamIds.includes(match.teamId);
  }

  return false;
}

function sortEvents() {
  eventsCache.sort((a, b) => Number(a.minute || 0) - Number(b.minute || 0));
}

function sortRatings() {
  ratingsCache.sort((a, b) => {
    return getPlayerNameFromRating(a).localeCompare(getPlayerNameFromRating(b));
  });
}

function setButtonLoading(button, isLoading, loadingText, defaultText) {
  if (!button) return;

  button.disabled = isLoading;
  button.textContent = isLoading ? loadingText : defaultText;
}

async function loadMatch() {
  const matchSnap = await getDoc(doc(db, "matches", matchId));

  if (!matchSnap.exists()) {
    if (reportTitle) reportTitle.textContent = "Match not found";
    return false;
  }

  currentMatch = { id: matchSnap.id, ...matchSnap.data() };

  if (currentMatch.organisationId !== activeProfile.organisationId) {
    showToast("This match does not belong to your organisation.", "error");
    window.location.href = "matches.html";
    return false;
  }

  if (!canAccessMatch(currentMatch)) {
    showToast("You do not have access to this post-match report.", "error");
    window.location.href = "matches.html";
    return false;
  }

  if (currentMatch.teamId) {
    const teamSnap = await getDoc(doc(db, "teams", currentMatch.teamId));

    if (teamSnap.exists()) {
      currentTeam = { id: teamSnap.id, ...teamSnap.data() };
    }
  }

  if (reportTitle) {
    reportTitle.textContent = `${currentTeam?.name || "Team"} vs ${currentMatch.opponent || "Opponent"}`;
  }

  if (reportMeta) {
    reportMeta.textContent = `${currentMatch.date || "No date"} • ${currentMatch.venue || "No venue"} • ${currentMatch.type || "match"}`;
  }

  if (reportHomeTeam) reportHomeTeam.textContent = currentTeam?.name || "Team";
  if (reportAwayTeam) reportAwayTeam.textContent = currentMatch.opponent || "Opponent";

  if (postMatchNotes) {
    postMatchNotes.value = currentMatch.postMatchNotes || "";
  }

  return true;
}

async function loadReportData() {
  const eventsQuery = query(
    collection(db, "matchEvents"),
    where("matchId", "==", matchId),
    where("organisationId", "==", activeProfile.organisationId)
  );

  const selectionQuery = query(
    collection(db, "matchSelections"),
    where("matchId", "==", matchId),
    where("organisationId", "==", activeProfile.organisationId)
  );

  const playersQuery = query(
    collection(db, "players"),
    where("organisationId", "==", activeProfile.organisationId)
  );

  const ratingsQuery = query(
    collection(db, "playerRatings"),
    where("matchId", "==", matchId),
    where("organisationId", "==", activeProfile.organisationId)
  );

  const [
    eventsSnapshot,
    selectionSnapshot,
    playersSnapshot,
    ratingsSnapshot
  ] = await Promise.all([
    getDocs(eventsQuery),
    getDocs(selectionQuery),
    getDocs(playersQuery),
    getDocs(ratingsQuery)
  ]);

  eventsCache = eventsSnapshot.docs.map((docSnap) => ({
    id: docSnap.id,
    ...docSnap.data()
  }));

  sortEvents();

  const selections = selectionSnapshot.docs.map((docSnap) => ({
    id: docSnap.id,
    ...docSnap.data()
  }));

  selections.sort((a, b) => getDateValue(b) - getDateValue(a));
  latestSelection = selections[0] || null;

  playersCache = playersSnapshot.docs
    .map((docSnap) => ({
      id: docSnap.id,
      ...docSnap.data()
    }))
    .filter((player) => {
      return getPlayerTeamIds(player).includes(currentMatch.teamId);
    });

  playersCache.sort((a, b) => {
    return getPlayerName(a).toLowerCase().localeCompare(getPlayerName(b).toLowerCase());
  });

  ratingsCache = ratingsSnapshot.docs.map((docSnap) => ({
    id: docSnap.id,
    ...docSnap.data()
  }));

  sortRatings();
}

function renderScoreStats() {
  let home = 0;
  let away = 0;

  eventsCache.forEach((event) => {
    const points = getEventPoints(event);

    if (event.team === "home") home += points;
    if (event.team === "away") away += points;
  });

  if (reportHomeScore) reportHomeScore.textContent = currentMatch.homeScore ?? home;
  if (reportAwayScore) reportAwayScore.textContent = currentMatch.awayScore ?? away;

  const tries = eventsCache.filter((event) => {
    return event.team === "home" && event.type === "try";
  }).length;

  const cards = eventsCache.filter((event) => {
    return event.type === "yellow_card" || event.type === "red_card";
  }).length;

  if (reportTries) reportTries.textContent = tries;
  if (reportCards) reportCards.textContent = cards;
}

function renderTeamMatchAnalytics() {
  const stats = currentMatch?.teamStats || {};

  const scrumsWon = Number(stats.scrumsWon || 0);
  const scrumsLost = Number(stats.scrumsLost || 0);

  const lineoutsWon = Number(stats.lineoutsWon || 0);
  const lineoutsLost = Number(stats.lineoutsLost || 0);

  const ruckFor = Number(stats.ruckTurnoversFor || 0);
  const ruckAgainst = Number(stats.ruckTurnoversAgainst || 0);

  const tacklesMade = Number(stats.tacklesMade || 0);
  const tacklesMissed = Number(stats.tacklesMissed || 0);

  if (reportScrumSuccess) reportScrumSuccess.textContent = getPercentage(scrumsWon, scrumsLost);
  if (reportScrumsWon) reportScrumsWon.textContent = scrumsWon;
  if (reportScrumsLost) reportScrumsLost.textContent = scrumsLost;

  if (reportLineoutSuccess) reportLineoutSuccess.textContent = getPercentage(lineoutsWon, lineoutsLost);
  if (reportLineoutsWon) reportLineoutsWon.textContent = lineoutsWon;
  if (reportLineoutsLost) reportLineoutsLost.textContent = lineoutsLost;

  if (reportRuckTurnoverBalance) reportRuckTurnoverBalance.textContent = ruckFor - ruckAgainst;
  if (reportRuckTurnoversFor) reportRuckTurnoversFor.textContent = ruckFor;
  if (reportRuckTurnoversAgainst) reportRuckTurnoversAgainst.textContent = ruckAgainst;

  if (reportTackleCompletion) reportTackleCompletion.textContent = getPercentage(tacklesMade, tacklesMissed);
  if (reportTacklesMade) reportTacklesMade.textContent = tacklesMade;
  if (reportTacklesMissed) reportTacklesMissed.textContent = tacklesMissed;
}

function renderSquadList(container, players, emptyMessage, fallbackStartNumber) {
  if (!container) return;

  container.innerHTML = "";

  if (!players?.length) {
    container.innerHTML = `<div class="list-item">${emptyMessage}</div>`;
    return;
  }

  players.forEach((player, index) => {
    const squadNumber = index + fallbackStartNumber;
    const role = player.selectedRole || player.position || "No position";

    const item = document.createElement("div");
    item.className = "squad-player-card";

    item.innerHTML = `
      <div>
        <strong>${squadNumber}. ${player.name || "Unknown Player"}</strong>
        <span>${role}</span>
      </div>
    `;

    container.appendChild(item);
  });
}

function renderSquad() {
  if (!latestSelection) {
    renderEmptySquad();
    return;
  }

  renderSquadList(
    reportStartingSquad,
    latestSelection.startingSquad || [],
    "No starting squad saved.",
    1
  );

  renderSquadList(
    reportBenchSquad,
    latestSelection.benchSquad || [],
    "No bench saved.",
    16
  );
}

function renderEmptySquad() {
  if (reportStartingSquad) {
    reportStartingSquad.innerHTML = `<div class="list-item">No starting squad saved.</div>`;
  }

  if (reportBenchSquad) {
    reportBenchSquad.innerHTML = `<div class="list-item">No bench saved.</div>`;
  }
}

function renderTimeline() {
  if (!reportTimeline) return;

  reportTimeline.innerHTML = "";

  if (!eventsCache.length) {
    reportTimeline.innerHTML = `<div class="list-item">No match events recorded.</div>`;
    return;
  }

  eventsCache.forEach((event) => {
    const item = document.createElement("div");
    item.className = "timeline-event elite-timeline-event";

    item.innerHTML = `
      <div class="timeline-minute">${event.minute}'</div>

      <div class="timeline-content">
        <h3>
          ${String(event.type || "").replace("_", " ").toUpperCase()}
          ${
            event.type === "conversion"
              ? ` • ${event.conversionResult === "missed" ? "MISSED" : "SUCCESSFUL"}`
              : ""
          }
        </h3>

        <p>
          ${event.team === "home" ? currentTeam?.name || "Team" : currentMatch.opponent || "Opponent"}
          ${event.playerName ? " • " + event.playerName : ""}
          ${event.notes ? " • " + event.notes : ""}
        </p>
      </div>
    `;

    reportTimeline.appendChild(item);
  });
}

function renderRatingPlayerDropdown() {
  if (!ratingPlayer) return;

  ratingPlayer.innerHTML = `<option value="">Select player</option>`;

  if (latestSelection) {
    const startingSquad = latestSelection.startingSquad || [];
    const benchSquad = latestSelection.benchSquad || [];

    if (startingSquad.length) {
      const group = document.createElement("optgroup");
      group.label = "Starting XV";

      startingSquad.forEach((player) => {
        const option = document.createElement("option");
        option.value = player.playerId;
        option.textContent = `${player.name || "Unknown Player"} — ${player.selectedRole || player.position || "No role"}`;
        group.appendChild(option);
      });

      ratingPlayer.appendChild(group);
    }

    if (benchSquad.length) {
      const group = document.createElement("optgroup");
      group.label = "Bench";

      benchSquad.forEach((player) => {
        const option = document.createElement("option");
        option.value = player.playerId;
        option.textContent = `${player.name || "Unknown Player"} — ${player.selectedRole || player.position || "No role"}`;
        group.appendChild(option);
      });

      ratingPlayer.appendChild(group);
    }

    if (startingSquad.length || benchSquad.length) {
      return;
    }
  }

  playersCache.forEach((player) => {
    const option = document.createElement("option");
    option.value = player.id;
    option.textContent = getPlayerName(player);
    ratingPlayer.appendChild(option);
  });
}

function renderPlayerRatings() {
  if (!playerRatingsList) return;

  playerRatingsList.innerHTML = "";

  if (!ratingsCache.length) {
    playerRatingsList.innerHTML = `<div class="list-item">No player ratings saved yet.</div>`;
    return;
  }

  ratingsCache.forEach((rating) => {
    const item = document.createElement("div");
    item.className = "list-item elite-readiness-row";

    item.innerHTML = `
      <div>
        <h3>${rating.playerName || "Unknown Player"}</h3>
        <p>${rating.comment || "No comment"}</p>
      </div>

      <div class="report-metrics">
        <span>${rating.rating}/10</span>

        <button class="btn-secondary edit-rating-btn" data-id="${rating.id}" type="button">
          Edit
        </button>

        <button class="btn-danger delete-rating-btn" data-id="${rating.id}" type="button">
          Delete
        </button>
      </div>
    `;

    playerRatingsList.appendChild(item);
  });
}

function renderAverageRating() {
  if (!reportAverageRating) return;

  if (!ratingsCache.length) {
    reportAverageRating.textContent = "0";
    return;
  }

  const total = ratingsCache.reduce((sum, rating) => {
    return sum + Number(rating.rating || 0);
  }, 0);

  reportAverageRating.textContent = (total / ratingsCache.length).toFixed(1);
}

function renderBestRatedPlayer() {
  if (!reportBestRating || !reportBestPlayer) return;

  if (!ratingsCache.length) {
    reportBestRating.textContent = "-";
    reportBestPlayer.textContent = "Top performer";
    return;
  }

  const bestRating = [...ratingsCache].sort((a, b) => {
    return Number(b.rating || 0) - Number(a.rating || 0);
  })[0];

  reportBestRating.textContent = `${bestRating.rating}/10`;
  reportBestPlayer.textContent = bestRating.playerName || "Top performer";
}

function renderMatchResult() {
  if (!reportResult) return;

  const home = Number(reportHomeScore?.textContent || currentMatch.homeScore || 0);
  const away = Number(reportAwayScore?.textContent || currentMatch.awayScore || 0);

  let result = currentMatch.result || "pending";

  if (!result || result === "pending") {
    if (home > away) result = "win";
    if (home < away) result = "loss";
    if (home === away && currentMatch.status === "Full Time") result = "draw";
  }

  reportResult.textContent = String(result || "pending").toUpperCase();
}

function renderPlayersOfTheMatch() {
  const forwards = [
    "Loosehead Prop",
    "Hooker",
    "Tighthead Prop",
    "Lock",
    "Flanker",
    "Number Eight",
    "Number 8"
  ];

  const backs = [
    "Scrumhalf",
    "Flyhalf",
    "Wing",
    "Inside Centre",
    "Outside Centre",
    "Centre",
    "Fullback"
  ];

  let bestForward = null;
  let bestBack = null;

  ratingsCache.forEach((rating) => {
    const squadPlayer =
      latestSelection?.startingSquad?.find((player) => player.playerId === rating.playerId) ||
      latestSelection?.benchSquad?.find((player) => player.playerId === rating.playerId);

    const role = squadPlayer?.selectedRole || squadPlayer?.position || "";

    if (forwards.includes(role)) {
      if (!bestForward || Number(rating.rating || 0) > Number(bestForward.rating || 0)) {
        bestForward = { ...rating, role };
      }
    }

    if (backs.includes(role)) {
      if (!bestBack || Number(rating.rating || 0) > Number(bestBack.rating || 0)) {
        bestBack = { ...rating, role };
      }
    }
  });

  if (forwardOfMatchName) forwardOfMatchName.textContent = bestForward?.playerName || "No forward ratings";
  if (forwardOfMatchPosition) forwardOfMatchPosition.textContent = bestForward?.role || "Forward performance will appear here.";
  if (forwardOfMatchRating) forwardOfMatchRating.textContent = bestForward ? `${bestForward.rating}/10` : "-";

  if (backOfMatchName) backOfMatchName.textContent = bestBack?.playerName || "No backline ratings";
  if (backOfMatchPosition) backOfMatchPosition.textContent = bestBack?.role || "Backline performance will appear here.";
  if (backOfMatchRating) backOfMatchRating.textContent = bestBack ? `${bestBack.rating}/10` : "-";
}

function renderMatchInsights() {
  if (!matchInsightsGrid) return;

  const homeTries = eventsCache.filter((event) => {
    return event.team === "home" && event.type === "try";
  }).length;

  const conversions = eventsCache.filter((event) => {
    return event.team === "home" && event.type === "conversion";
  });

  const successfulConversions = conversions.filter((event) => {
    return event.conversionResult === "successful";
  }).length;

  const conversionRate = conversions.length
    ? Math.round((successfulConversions / conversions.length) * 100)
    : 0;

  const cards = eventsCache.filter((event) => {
    return event.type === "yellow_card" || event.type === "red_card";
  }).length;

  const topRated = [...ratingsCache].sort((a, b) => {
    return Number(b.rating || 0) - Number(a.rating || 0);
  })[0];

  const insights = [
    {
      title: "Finishing",
      text: `${homeTries} tries scored. ${homeTries >= 3 ? "Strong attacking output." : "Attack conversion can improve."}`
    },
    {
      title: "Goal Kicking",
      text: conversions.length
        ? `${conversionRate}% conversion success from ${conversions.length} attempt(s).`
        : "No conversion attempts recorded."
    },
    {
      title: "Discipline",
      text: cards
        ? `${cards} card event(s) recorded. Discipline review recommended.`
        : "No card events recorded."
    },
    {
      title: "Impact Player",
      text: topRated
        ? `${topRated.playerName} was the highest rated player with ${topRated.rating}/10.`
        : "No player ratings captured yet."
    }
  ];

  matchInsightsGrid.innerHTML = "";

  insights.forEach((insight) => {
    const item = document.createElement("div");
    item.className = "insight-card";

    item.innerHTML = `
      <h3>${insight.title}</h3>
      <p>${insight.text}</p>
    `;

    matchInsightsGrid.appendChild(item);
  });
}

function renderCoachSummary() {
  if (!coachSummary) return;

  const homeScore = Number(reportHomeScore?.textContent || 0);
  const awayScore = Number(reportAwayScore?.textContent || 0);
  const tries = eventsCache.filter((event) => event.team === "home" && event.type === "try").length;
  const cards = eventsCache.filter((event) => event.type === "yellow_card" || event.type === "red_card").length;

  let resultText = "The match outcome is still pending.";

  if (homeScore > awayScore) {
    resultText = "The team secured a positive result.";
  }

  if (homeScore < awayScore) {
    resultText = "The team will need to review key performance areas after the loss.";
  }

  if (homeScore === awayScore && currentMatch.status === "Full Time") {
    resultText = "The match ended level.";
  }

  coachSummary.textContent =
    `${resultText} The team scored ${tries} try/tries and recorded ${cards} card event(s). ` +
    `Player ratings and match events should be reviewed to identify selection, tactical and training focus areas for the next fixture.`;
}

function renderMatchGrade() {
  if (!reportMatchGrade || !reportMatchGradeText) return;

  const homeScore = Number(reportHomeScore?.textContent || 0);
  const awayScore = Number(reportAwayScore?.textContent || 0);
  const tries = eventsCache.filter((event) => event.team === "home" && event.type === "try").length;
  const cards = eventsCache.filter((event) => event.type === "yellow_card" || event.type === "red_card").length;

  const averageRating = ratingsCache.length
    ? ratingsCache.reduce((sum, rating) => sum + Number(rating.rating || 0), 0) / ratingsCache.length
    : 0;

  let grade = "C";
  let text = "Solid performance with clear improvement areas.";

  if (homeScore > awayScore && tries >= 4 && averageRating >= 8 && cards === 0) {
    grade = "A";
    text = "Excellent team performance across score, attack, discipline and player ratings.";
  } else if (homeScore >= awayScore && averageRating >= 7) {
    grade = "B";
    text = "Good performance with positive match control and player contribution.";
  } else if (homeScore < awayScore || averageRating < 6 || cards >= 2) {
    grade = "C";
    text = "Performance review required. Focus on execution, discipline and key player roles.";
  }

  reportMatchGrade.textContent = grade;
  reportMatchGradeText.textContent = text;
}

function renderReportStatus() {
  if (!reportStatus || !reportStatusText) return;

  const isComplete = currentMatch.postMatchReportStatus === "complete";

  reportStatus.textContent = isComplete ? "Complete" : "Draft";
  reportStatusText.textContent = isComplete
    ? "Post-match report has been completed"
    : "Post-match report not completed yet";
}

function applyReportLockState() {
  const isComplete = currentMatch.postMatchReportStatus === "complete";
  const isDirector = activeProfile?.role === "director";

  if (playerRatingForm) {
    playerRatingForm.querySelectorAll("input, select, button").forEach((field) => {
      field.disabled = isComplete;
    });
  }

  if (postMatchNotes) {
    postMatchNotes.disabled = isComplete;
  }

  if (postMatchNotesForm) {
    postMatchNotesForm.querySelectorAll("button").forEach((button) => {
      button.disabled = isComplete;
    });
  }

  if (saveReportStatusBtn) {
    saveReportStatusBtn.disabled = isComplete;
    saveReportStatusBtn.textContent = isComplete ? "Report Complete" : "Mark Report Complete";
  }

  if (unlockReportBtn) {
    unlockReportBtn.classList.toggle("hidden", !(isComplete && isDirector));
  }
}

function renderPage() {
  renderScoreStats();
  renderTeamMatchAnalytics();
  renderSquad();
  renderTimeline();
  renderRatingPlayerDropdown();
  renderPlayerRatings();
  renderAverageRating();
  renderBestRatedPlayer();
  renderMatchResult();
  renderPlayersOfTheMatch();
  renderMatchInsights();
  renderCoachSummary();
  renderMatchGrade();
  renderReportStatus();
  applyReportLockState();
}

printPostMatchBtn?.addEventListener("click", function () {
  printPostMatchBtn.disabled = true;
  printPostMatchBtn.textContent = "Preparing...";

  showLoader("Preparing print report...");

  const restorePrintButton = () => {
    hideLoader();

    printPostMatchBtn.disabled = false;
    printPostMatchBtn.textContent = "Print Report";

    window.removeEventListener("afterprint", restorePrintButton);
  };

  window.addEventListener("afterprint", restorePrintButton);

  setTimeout(() => {
    window.print();
  }, 150);
});

postMatchNotesForm?.addEventListener("submit", async function (event) {
  event.preventDefault();

  try {
    const notes = postMatchNotes?.value.trim() || "";

    await updateDoc(doc(db, "matches", matchId), {
      postMatchNotes: notes,
      postMatchNotesUpdatedAt: serverTimestamp()
    });

    currentMatch.postMatchNotes = notes;

    showToast("Post-match notes saved successfully.", "success");
  } catch (error) {
    console.error("Error saving post-match notes:", error);
    showToast("Could not save notes: " + error.message, "error");
  }
});

function exportPostMatchCSV() {
  let csvContent = "";

  csvContent += "POST MATCH SUMMARY\n";
  csvContent += `"Team","${currentTeam?.name || "Team"}"\n`;
  csvContent += `"Opponent","${currentMatch?.opponent || "Opponent"}"\n`;
  csvContent += `"Date","${currentMatch?.date || ""}"\n`;
  csvContent += `"Home Score","${reportHomeScore?.textContent || "0"}"\n`;
  csvContent += `"Away Score","${reportAwayScore?.textContent || "0"}"\n`;
  csvContent += `"Result","${reportResult?.textContent || "-"}"\n`;
  csvContent += `"Match Grade","${reportMatchGrade?.textContent || "-"}"\n`;
  csvContent += `"Grade Summary","${String(reportMatchGradeText?.textContent || "").replace(/"/g, '""')}"\n`;
  csvContent += `"Coach Summary","${String(coachSummary?.textContent || "").replace(/"/g, '""')}"\n\n`;
  
  const teamStats = currentMatch?.teamStats || {};

  csvContent += "TEAM MATCH ANALYTICS\n";
  csvContent += `"Scrums Won","${teamStats.scrumsWon || 0}"\n`;
  csvContent += `"Scrums Lost","${teamStats.scrumsLost || 0}"\n`;
  csvContent += `"Lineouts Won","${teamStats.lineoutsWon || 0}"\n`;
  csvContent += `"Lineouts Lost","${teamStats.lineoutsLost || 0}"\n`;
  csvContent += `"Ruck Turnovers For","${teamStats.ruckTurnoversFor || 0}"\n`;
  csvContent += `"Ruck Turnovers Against","${teamStats.ruckTurnoversAgainst || 0}"\n`;
  csvContent += `"Tackles Made","${teamStats.tacklesMade || 0}"\n`;
  csvContent += `"Tackles Missed","${teamStats.tacklesMissed || 0}"\n\n`;

  csvContent += "MATCH EVENTS\n";
  csvContent += "Minute,Team,Event Type,Conversion Result,Points,Player,Notes\n";

  eventsCache.forEach((event) => {
    const row = [
      `"${event.minute || ""}"`,
      `"${event.team === "home" ? currentTeam?.name || "Team" : currentMatch.opponent || "Opponent"}"`,
      `"${event.type || ""}"`,
      `"${event.conversionResult || ""}"`,
      `"${getEventPoints(event)}"`,
      `"${event.playerName || ""}"`,
      `"${String(event.notes || "").replace(/"/g, '""')}"`
    ];

    csvContent += row.join(",") + "\n";
  });

  csvContent += "\nPLAYER RATINGS\n";
  csvContent += "Player,Rating,Comment\n";

  ratingsCache.forEach((rating) => {
    const row = [
      `"${rating.playerName || ""}"`,
      `"${rating.rating || ""}"`,
      `"${String(rating.comment || "").replace(/"/g, '""')}"`
    ];

    csvContent += row.join(",") + "\n";
  });

  csvContent += "\nPLAYERS OF THE MATCH\n";
  csvContent += "Category,Player,Position,Rating\n";
  csvContent += `"Forward of the Match","${forwardOfMatchName?.textContent || ""}","${forwardOfMatchPosition?.textContent || ""}","${forwardOfMatchRating?.textContent || ""}"\n`;
  csvContent += `"Backline Player of the Match","${backOfMatchName?.textContent || ""}","${backOfMatchPosition?.textContent || ""}","${backOfMatchRating?.textContent || ""}"\n`;

  const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
  const link = document.createElement("a");
  const url = URL.createObjectURL(blob);

  link.setAttribute("href", url);
  link.setAttribute(
    "download",
    `post-match-report-${currentMatch?.date || "report"}.csv`
  );

  link.style.visibility = "hidden";

  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);

  URL.revokeObjectURL(url);
}

exportPostMatchCsvBtn?.addEventListener("click", exportPostMatchCSV);

saveReportStatusBtn?.addEventListener("click", async function () {
  setButtonLoading(saveReportStatusBtn, true, "Completing...", "Mark Report Complete");

  try {
    await updateDoc(doc(db, "matches", matchId), {
      postMatchReportStatus: "complete",
      postMatchReportCompletedAt: serverTimestamp(),
      postMatchReportCompletedBy: activeProfile.uid,
      updatedAt: serverTimestamp()
    });

    currentMatch.postMatchReportStatus = "complete";

    renderReportStatus();
    applyReportLockState();

    showToast("Post-match report marked as complete.", "success");
  } catch (error) {
    console.error("Error completing report:", error);
    showToast("Could not complete report: " + error.message, "error");
  } finally {
    if (currentMatch?.postMatchReportStatus !== "complete") {
      setButtonLoading(saveReportStatusBtn, false, "Completing...", "Mark Report Complete");
    }
  }
});

unlockReportBtn?.addEventListener("click", async function () {
  const confirmed = confirm("Are you sure you want to unlock this completed report?");
  if (!confirmed) return;

  setButtonLoading(unlockReportBtn, true, "Unlocking...", "Unlock Report");

  try {
    await updateDoc(doc(db, "matches", matchId), {
      postMatchReportStatus: "draft",
      postMatchReportUnlockedAt: serverTimestamp(),
      postMatchReportUnlockedBy: activeProfile.uid,
      updatedAt: serverTimestamp()
    });

    currentMatch.postMatchReportStatus = "draft";

    renderReportStatus();
    applyReportLockState();

    showToast("Post-match report unlocked.", "success");
  } catch (error) {
    console.error("Error unlocking report:", error);
    showToast("Could not unlock report: " + error.message, "error");
  } finally {
    setButtonLoading(unlockReportBtn, false, "Unlocking...", "Unlock Report");
  }
});

playerRatingForm?.addEventListener("submit", async function (event) {
  event.preventDefault();

  const playerId = ratingPlayer?.value || "";

  const selectedPlayer =
    playersCache.find((player) => player.id === playerId) ||
    {
      id: playerId,
      firstName: ratingPlayer?.options[ratingPlayer.selectedIndex]?.textContent || "",
      lastName: ""
    };

  if (!playerId) {
    showToast("Please select a player.", "warning");
    return;
  }

  const ratingValue = Number(ratingScore?.value || 0);

  if (ratingValue < 1 || ratingValue > 10) {
    showToast("Rating must be between 1 and 10.", "warning");
    return;
  }

  const ratingData = {
    matchId,
    teamId: currentMatch.teamId,
    teamIds: getPlayerTeamIds(selectedPlayer),
    teamName: currentTeam?.name || "",
    teamNames: selectedPlayer.teamNames || [],
    playerId: selectedPlayer.id,
    playerName: getPlayerName(selectedPlayer),
    rating: ratingValue,
    comment: ratingComment?.value.trim() || "",
    organisationId: activeProfile.organisationId,
    updatedAt: serverTimestamp()
  };

  setButtonLoading(
    ratingSubmitButton,
    true,
    editingRatingId ? "Updating..." : "Saving...",
    editingRatingId ? "Update Rating" : "Save Rating"
  );

  try {
    if (editingRatingId) {
      await updateDoc(doc(db, "playerRatings", editingRatingId), ratingData);

      ratingsCache = ratingsCache.map((rating) =>
        rating.id === editingRatingId
          ? {
              ...rating,
              ...ratingData,
              updatedAt: new Date()
            }
          : rating
      );

      showToast("Player rating updated successfully.", "success");
    } else {
      const ratingRef = await addDoc(collection(db, "playerRatings"), {
        ...ratingData,
        createdBy: activeProfile.uid,
        createdAt: serverTimestamp()
      });

      ratingsCache.push({
        id: ratingRef.id,
        ...ratingData,
        createdBy: activeProfile.uid,
        createdAt: new Date()
      });

      showToast("Player rating saved successfully.", "success");
    }

    editingRatingId = null;
    playerRatingForm.reset();

    if (ratingSubmitButton) {
      ratingSubmitButton.textContent = "Save Rating";
    }

    sortRatings();
    renderPlayerRatings();
    renderAverageRating();
    renderBestRatedPlayer();
    renderPlayersOfTheMatch();
    renderMatchInsights();
    renderCoachSummary();
    renderMatchGrade();
  } catch (error) {
    console.error("Error saving player rating:", error);
    showToast("Could not save player rating: " + error.message, "error");
  } finally {
    setButtonLoading(ratingSubmitButton, false, "Saving...", "Save Rating");
  }
});

playerRatingsList?.addEventListener("click", async function (event) {
  const editBtn = event.target.closest(".edit-rating-btn");
  const deleteBtn = event.target.closest(".delete-rating-btn");

  if (editBtn) {
    const ratingId = editBtn.dataset.id;
    const rating = ratingsCache.find((item) => item.id === ratingId);

    if (!rating) return;

    editingRatingId = rating.id;

    if (ratingPlayer) ratingPlayer.value = rating.playerId || "";
    if (ratingScore) ratingScore.value = rating.rating || "";
    if (ratingComment) ratingComment.value = rating.comment || "";
    if (ratingSubmitButton) ratingSubmitButton.textContent = "Update Rating";

    window.scrollTo({ top: 0, behavior: "smooth" });
  }

  if (deleteBtn) {
    const ratingId = deleteBtn.dataset.id;

    const confirmed = confirm("Are you sure you want to delete this player rating?");
    if (!confirmed) return;

    setButtonLoading(deleteBtn, true, "Deleting...", "Delete");

    try {
      await deleteDoc(doc(db, "playerRatings", ratingId));

      ratingsCache = ratingsCache.filter((rating) => rating.id !== ratingId);

      renderPlayerRatings();
      renderAverageRating();
      renderBestRatedPlayer();
      renderPlayersOfTheMatch();
      renderMatchInsights();
      renderCoachSummary();
      renderMatchGrade();

      showToast("Player rating deleted.", "success");
    } catch (error) {
      console.error("Error deleting player rating:", error);
      showToast("Could not delete rating: " + error.message, "error");
    } finally {
      setButtonLoading(deleteBtn, false, "Deleting...", "Delete");
    }
  }
});

requireAuth(async function (user, profile) {
  activeProfile = profile;
  setupLogoutButton();

  if (!matchId) {
    if (reportTitle) reportTitle.textContent = "Invalid Match";
    return;
  }

  showLoader("Loading post-match report...");

  if (reportTimeline) {
    reportTimeline.innerHTML = `<div class="list-item">Loading post-match report...</div>`;
  }

  try {
    const loaded = await loadMatch();

    if (!loaded || !currentMatch) return;

    await loadReportData();
    renderPage();
  } catch (error) {
    console.error("Post-match report error:", error);

    if (reportTitle) {
      reportTitle.textContent = "Could not load post-match report";
    }

    if (reportTimeline) {
      reportTimeline.innerHTML = `<div class="list-item">Error: ${error.message}</div>`;
    }

    showToast("Could not load post-match report: " + error.message, "error");
  } finally {
    hideLoader();
  }
});