<template>
  <v-card class="ma-1">
    <v-card-title>Group Repeatability Summary</v-card-title>
    <v-card-text>
      <p>Inline standard deviation (σ_x) for each baseline-monitor pair. Lower values indicate better repeatability. Colors range from green (best) to red (worst).</p>
      <v-simple-table dense>
        <thead>
          <tr>
            <th>Baseline \ Monitor</th>
            <th v-for="project in projects" :key="project.pid">{{ project.pid }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(baselineProject, rowIndex) in projects" :key="baselineProject.pid">
            <td>{{ baselineProject.pid }}</td>
            <td v-for="(monitorProject, colIndex) in projects" :key="monitorProject.pid">
              <v-tooltip v-if="colIndex > rowIndex" top>
                <template v-slot:activator="{ on, attrs }">
                  <div
                    :style="{ backgroundColor: getSigmaXColor(baselineProject.pid, monitorProject.pid), color: 'white', textAlign: 'center', padding: '4px' }"
                    v-bind="attrs"
                    v-on="on"
                    @click="emitInput(baselineProject, monitorProject)"
                  >
                    {{ formatSigmaX(baselineProject.pid, monitorProject.pid) }}
                  </div>
                </template>
                <span v-if="getComp(baselineProject.pid, monitorProject.pid)">
                  <div>σ_x: {{ getComp(baselineProject.pid, monitorProject.pid).meta['σ'][0].toFixed(2) }} m</div>
                  <div>σ_y: {{ getComp(baselineProject.pid, monitorProject.pid).meta['σ'][1].toFixed(2) }} m</div>
                  <div>Anisotropy: {{ getComp(baselineProject.pid, monitorProject.pid).meta.anisotropy.toFixed(0) }}</div>
                  <div>Primary Direction: {{ formatPrimaryDirection(getComp(baselineProject.pid, monitorProject.pid)) }}°</div>
                </span>
              </v-tooltip>
            </td>
          </tr>
        </tbody>
      </v-simple-table>
    </v-card-text>
  </v-card>
</template>

<script>
export default {
  name: 'DougalGroupRepeatabilitySummary',

  props: {
    comparisons: {
      type: Array,
      required: true
    },
    projects: {
      type: Array,
      required: true
    }
  },

  data () {
    return {
    };
  },

  computed: {
    compMap () {
      return new Map(this.comparisons.map(c => [`${c.baseline_pid}-${c.monitor_pid}`, c]));
    },
    minSigmaX () {
      if (!this.comparisons.length) return 0;
      return Math.min(...this.comparisons.map(c => c.meta['σ'][0]));
    },
    maxSigmaX () {
      if (!this.comparisons.length) return 0;
      return Math.max(...this.comparisons.map(c => c.meta['σ'][0]));
    }
  },
  methods: {
    getComp (basePid, monPid) {
      return this.compMap.get(`${basePid}-${monPid}`);
    },
    getSigmaX (basePid, monPid) {
      return this.getComp(basePid, monPid)?.meta?.['σ']?.[0] ?? null;
    },
    formatSigmaX (basePid, monPid) {
      const val = this.getSigmaX(basePid, monPid);
      return val !== null ? val.toFixed(1) : '';
    },
    getSigmaXColor (basePid, monPid) {
      const val = this.getSigmaX(basePid, monPid);
      if (val === null) return '';
      const ratio = (val - this.minSigmaX) / (this.maxSigmaX - this.minSigmaX);
      const hue = (1 - ratio) * 120;
      return `hsl(${hue}, 70%, 70%)`;
    },
    formatPrimaryDirection (comp) {
      if (!comp) return '';
      return (comp.meta.primaryDirection * 180 / Math.PI).toFixed(1);
    },
    emitInput (baselineProject, monitorProject) {
      if (this.getComp(baselineProject.pid, monitorProject.pid)) {
        this.$emit('input', baselineProject, monitorProject);
      }
    }
  }
}
</script>
