<script setup lang="ts">
import { computed, ref } from 'vue'
import QueryStateBoundary from '../../../shared/ui/QueryStateBoundary.vue'
import PolicyTracePanel from '../components/PolicyTracePanel.vue'
import { useEvaluateResearchSellPolicy } from '../queries'
import type { ResearchSellPolicyCommand } from '../api'

const mutation = useEvaluateResearchSellPolicy()
const hardImpairmentApproved = ref(false)
const capitalFloorBreached = ref(false)
const gapBelowFloorAtr = ref(1.6)
const consecutiveCloseBreaches = ref(0)
const lastCommand = ref<ResearchSellPolicyCommand | null>(null)

const isBusy = computed(() => mutation.isPending.value)

function createCommand(): ResearchSellPolicyCommand {
  const asOf = new Date().toISOString()
  return {
    idempotencyKey: crypto.randomUUID(),
    request: {
      positionLotId: '00000000-0000-0000-0000-000000000001',
      cycleId: '00000000-0000-0000-0000-000000000002',
      evidenceId: 'sample-evidence',
      datasetId: 'sample-dataset',
      modelVersion: 'research-v12.2',
      configVersion: 'proposal-v12.2',
      codeSha: 'sample-code-sha',
      asOf,
      publishedAtCutoff: asOf,
      currentSecurityPortfolioWeight: 0.6,
      currentLotPortfolioWeight: 0.2,
      strategicCoreFloorWeight: 0.3,
      hardImpairmentApproved: hardImpairmentApproved.value,
      capitalFloorBreached: capitalFloorBreached.value,
      survivalSellRatioOfLot: 0.5,
      gapBelowFloorAtr: gapBelowFloorAtr.value,
      consecutiveCloseBreaches: consecutiveCloseBreaches.value,
      cooldownSatisfied: true,
      concentrationSellRatioOfLot: 0,
      opportunityEdgeLowerBound: 0,
      opportunitySellRatioOfLot: 0
    }
  }
}

function run() {
  const command = createCommand()
  lastCommand.value = command
  mutation.mutate(command)
}

function retry() {
  if (lastCommand.value) mutation.mutate(lastCommand.value)
}
</script>

<template>
  <main>
    <header>
      <h1>매도 정책 연구 콘솔</h1>
      <p>
        순수 정책 계약과 우선순위를 확인하는 연구 전용 화면입니다.
        고객 제안·공개·주문 기능과 연결되지 않습니다.
      </p>
      <strong>RESEARCH_CANDIDATE_NOT_PRODUCTION · 자동주문 OFF</strong>
    </header>

    <form @submit.prevent="run">
      <fieldset :disabled="isBusy">
        <legend>연구 입력 벡터</legend>
        <label>
          <input v-model="hardImpairmentApproved" type="checkbox" />
          Hard impairment 승인
        </label>
        <label>
          <input v-model="capitalFloorBreached" type="checkbox" />
          자본바닥 위반
        </label>
        <label>
          보호선 이탈 ATR
          <input v-model.number="gapBelowFloorAtr" type="number" min="0" step="0.1" />
        </label>
        <label>
          연속 종가 이탈
          <input v-model.number="consecutiveCloseBreaches" type="number" min="0" step="1" />
        </label>
        <button type="submit">정책 평가</button>
      </fieldset>
    </form>

    <QueryStateBoundary
      :loading="isBusy"
      :error="mutation.error.value as Error | null"
      :empty="!mutation.data.value"
      @retry="retry"
    >
      <dl v-if="mutation.data.value">
        <dt>행동</dt><dd>{{ mutation.data.value.action }}</dd>
        <dt>정책</dt><dd>{{ mutation.data.value.policyId }}</dd>
        <dt>사유</dt><dd>{{ mutation.data.value.reasonCode }}</dd>
        <dt>Lot 매도비율</dt><dd>{{ mutation.data.value.sellRatioOfLot }}</dd>
        <dt>매도 후 종목 비중</dt><dd>{{ mutation.data.value.targetSecurityPortfolioWeightAfter }}</dd>
        <dt>재진입 가능</dt><dd>{{ mutation.data.value.reentryEligible }}</dd>
        <dt>결정 계약</dt><dd>{{ mutation.data.value.decisionContractVersion }}</dd>
        <dt>정책 추적</dt><dd>{{ mutation.data.value.policyTrace.length }}단계</dd>
      </dl>
      <PolicyTracePanel
        v-if="mutation.data.value"
        :entries="mutation.data.value.policyTrace"
        :schema-version="mutation.data.value.policyTraceSchemaVersion"
      />
    </QueryStateBoundary>
  </main>
</template>
