import { api } from '../../shared/api/client'
import { modelOperationsPlanSchema, type ModelOperationsPlan } from './schema'

export const mockModelOperationsPlan: ModelOperationsPlan = {
  algorithmStatus: 'RESEARCH_CANDIDATE_NOT_PRODUCTION',
  orderCapability: 'AUTOMATIC_ORDER_AND_KIS_SUBMISSION_OFF',
  modelMutationBoundary: 'EVALUATION_AND_PROPOSAL_ONLY_HUMAN_APPROVAL_REQUIRED',
  operations: [
    {
      operationCode: 'OP-EVAL-001',
      name: 'Shadow Run 일일 알고리즘 평가',
      cadence: 'DAILY',
      automationMode: 'EVALUATION_ONLY',
      queue: 'shadow-run-daily',
      primaryOwner: 'QuantOps-Team',
      secondaryOwner: 'Risk-Control',
      requiredEvidence: 'SHA-256 Code/Model/Dataset VersionSet',
      output: 'PBO/DSR Validation Metrics',
      gate: 'G2 Evaluation Pass'
    },
    {
      operationCode: 'OP-PROP-002',
      name: '리밸런싱 제안 생성 드립',
      cadence: 'WEEKLY',
      automationMode: 'PROPOSAL_ONLY',
      queue: 'rebalance-proposal',
      primaryOwner: 'Portfolio-Manager',
      secondaryOwner: 'Governance-Board',
      requiredEvidence: 'Target Portfolio Weight Delta Snapshot',
      output: 'Rebalance Order Intent (Blocked KIS)',
      gate: 'Maker-Checker Approval'
    },
    {
      operationCode: 'OP-DRILL-003',
      name: '오프라인 장애 복구 훈련',
      cadence: 'MONTHLY',
      automationMode: 'DRILL_ONLY',
      queue: 'disaster-recovery-drill',
      primaryOwner: 'Site-Reliability',
      secondaryOwner: 'DevOps-Lead',
      requiredEvidence: 'PIT State Replay Audit',
      output: 'Failover Evidence Snapshot',
      gate: 'G5 Operational Pass'
    }
  ]
}

export async function getModelOperationsPlan(): Promise<ModelOperationsPlan> {
  try {
    const response = await api.get('/internal/v1/model-operations/plan')
    return modelOperationsPlanSchema.parse(response.data)
  } catch (error) {
    console.warn('[ModelOperations] 백엔드 API 부재(502/Network Error)로 표준 Mock 데이터를 사용합니다.', error)
    return mockModelOperationsPlan
  }
}
