import { Router } from 'express'
import { query, withTransaction } from '../config/database.js'
import { sendSuccess, sendError, parseMonth, parsePagination } from '../middleware/error.js'
import type { AuthRequest } from '../middleware/auth.js'

const router = Router()

const RECEIVED_COLS = `COALESCE(c143::numeric,0)+COALESCE(c144::numeric,0)+COALESCE(c145::numeric,0)+COALESCE(c146::numeric,0)+COALESCE(c147::numeric,0)+COALESCE(c148::numeric,0)+COALESCE(c149::numeric,0)+COALESCE(c150::numeric,0)+COALESCE(c151::numeric,0)+COALESCE(c152::numeric,0)+COALESCE(c153::numeric,0)+COALESCE(c155::numeric,0)+COALESCE(c160::numeric,0)+COALESCE(c161::numeric,0)+COALESCE(c164::numeric,0)+COALESCE(c166::numeric,0)+COALESCE(c169::numeric,0)+COALESCE(c173::numeric,0)+COALESCE(c174::numeric,0)+COALESCE(c118::numeric,0)+COALESCE(c119::numeric,0)+COALESCE(c120::numeric,0)+COALESCE(c121::numeric,0)+COALESCE(c139::numeric,0)+COALESCE(c141::numeric,0)+COALESCE(c142::numeric,0)`

// ============================================================
// 固定路径路由（必须在 /:id 之前定义）
// ============================================================

router.get('/', async (req: AuthRequest, res) => {
  try {
    const { page, pageSize, offset } = parsePagination(req)
    const month = parseMonth(req)
    const priority = req.query.priority as string
    const status = req.query.status as string
    const storeCode = req.query.store_code as string

    const conditions: string[] = [`plan_month = $1`]
    const params: any[] = [month]
    let paramIdx = 2

    // 默认排除模拟验收记录，除非显式请求
    const includeSimulated = req.query.include_simulated === 'true'
    if (!includeSimulated) {
      conditions.push(`(is_simulated = false OR is_simulated IS NULL)`)
    }

    if (priority) {
      conditions.push(`priority = $${paramIdx++}`)
      params.push(priority)
    }
    if (status) {
      conditions.push(`status = $${paramIdx++}`)
      params.push(status)
    }
    if (storeCode) {
      conditions.push(`store_code = $${paramIdx++}`)
      params.push(storeCode)
    }

    const where = conditions.join(' AND ')
    const countResult = await query(`SELECT count(*) AS total FROM analytics.store_task WHERE ${where}`, params)
    const result = await query(
      `SELECT * FROM analytics.store_task WHERE ${where} ORDER BY
        CASE priority WHEN 'P0-修复数据口径' THEN 1 WHEN 'P0-综合专项整改' THEN 2 WHEN 'P1' THEN 3 WHEN 'P2' THEN 4 ELSE 5 END,
        deadline ASC
       LIMIT $${paramIdx++} OFFSET $${paramIdx++}`,
      [...params, pageSize, offset]
    )
    sendSuccess(res, result.rows, { total: parseInt(countResult.rows[0].total), page, page_size: pageSize })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/', async (req: AuthRequest, res) => {
  try {
    const b = req.body
    const result = await withTransaction(async (client) => {
      const taskResult = await client.query(`
        INSERT INTO analytics.store_task
          (plan_month, store_code, store_name, priority, problem_indicator,
           current_value, benchmark_value, target_value, problem_description,
           action_required, owner, collaborators, deadline,
           verification_indicator)
        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
        RETURNING task_id
      `, [b.plan_month, b.store_code, b.store_name, b.priority, b.problem_indicator,
           b.current_value, b.benchmark_value, b.target_value, b.problem_description,
           b.action_required, b.owner, b.collaborators, b.deadline, b.verification_indicator])

      const taskId = taskResult.rows[0].task_id
      await client.query(`
        INSERT INTO analytics.store_task_log (task_id, action, new_status, operator, comment)
        VALUES ($1, '创建', '待启动', $2, $3)
      `, [taskId, req.user?.name || 'system', b.problem_description])

      return taskId
    })
    sendSuccess(res, { task_id: result })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/auto-generate', async (req: AuthRequest, res) => {
  try {
    const month = req.body.month ? (req.body.month.length === 7 ? `${req.body.month}-01` : req.body.month) : parseMonth(req)
    const result = await query(`SELECT * FROM analytics.f_generate_store_tasks($1)`, [month])
    sendSuccess(res, result.rows[0] || { generated: 0, message: 'No tasks generated' })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/weekly-check', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`SELECT * FROM analytics.v_task_weekly_check WHERE plan_month = $1 ORDER BY consecutive_no_improve_weeks DESC, store_code`, [month])
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/monthly-review', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`SELECT * FROM analytics.v_task_monthly_review WHERE plan_month = $1 ORDER BY store_code`, [month])
    const summary = {
      total: result.rows.length,
      passed: result.rows.filter((r: any) => r.review_result === '达标').length,
      improving: result.rows.filter((r: any) => r.review_result === '改善中').length,
      failed: result.rows.filter((r: any) => r.review_result === '未改善').length,
    }
    sendSuccess(res, { summary, details: result.rows })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/followup', async (req: AuthRequest, res) => {
  try {
    const month = req.query.month as string | undefined
    if (month) {
      const monthDate = month + '-01'
      const result = await query(`SELECT * FROM analytics.mv_store_monthly_followup WHERE plan_month = $1::date ORDER BY priority, store_code`, [monthDate])
      sendSuccess(res, result.rows)
    } else {
      const result = await query(`SELECT * FROM analytics.mv_store_monthly_followup ORDER BY priority, store_code`)
      sendSuccess(res, result.rows)
    }
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/grade-change', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`SELECT * FROM analytics.store_grade_change WHERE change_month = $1::date ORDER BY change_month DESC, store_code`, [month])
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/practices', async (req: AuthRequest, res) => {
  try {
    const mod = req.query.module as string
    const status = req.query.status as string
    let sql = `SELECT * FROM analytics.standardized_practice`
    const params: any[] = []
    const conditions: string[] = []
    if (mod) {
      conditions.push(`practice_module = $${params.length + 1}`)
      params.push(mod)
    }
    if (status) {
      conditions.push(`status = $${params.length + 1}`)
      params.push(status)
    }
    if (conditions.length > 0) {
      sql += ` WHERE ` + conditions.join(' AND ')
    }
    sql += ` ORDER BY created_at DESC`
    const result = await query(sql, params)
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/practices', async (req: AuthRequest, res) => {
  try {
    const b = req.body
    const result = await query(`
      INSERT INTO analytics.standardized_practice
        (practice_module, benchmark_store_code, benchmark_store_name, key_actions, verification_indicators)
      VALUES ($1, $2, $3, $4, $5) RETURNING id
    `, [b.practice_module, b.benchmark_store_code, b.benchmark_store_name, b.key_actions, b.verification_indicators])
    sendSuccess(res, { id: result.rows[0].id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/practices/:id/replication-result', async (req: AuthRequest, res) => {
  try {
    const practiceId = parseInt(req.params.id)
    const result = await query(`SELECT * FROM analytics.practice_replication WHERE practice_id = $1 ORDER BY id`, [practiceId])
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/practices/:id/replicate', async (req: AuthRequest, res) => {
  try {
    const practiceId = parseInt(req.params.id)
    const b = req.body
    const result = await query(`
      INSERT INTO analytics.practice_replication
        (practice_id, trial_store_code, trial_store_name, observation_weeks, before_value, status)
      VALUES ($1, $2, $3, $4, $5, '观察中') RETURNING id
    `, [practiceId, b.trial_store_code, b.trial_store_name, b.observation_weeks || 4, b.before_value])
    sendSuccess(res, { id: result.rows[0].id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/practices/:id/promote', async (req: AuthRequest, res) => {
  try {
    const practiceId = parseInt(req.params.id)
    await query(`UPDATE analytics.standardized_practice SET status = '已推广' WHERE id = $1`, [practiceId])
    sendSuccess(res, { id: practiceId, status: '已推广' })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/indicators', async (req: AuthRequest, res) => {
  try {
    const result = await query(`SELECT * FROM analytics.indicator_dictionary ORDER BY id`)
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/indicators', async (req: AuthRequest, res) => {
  try {
    const b = req.body
    const result = await query(`
      INSERT INTO analytics.indicator_dictionary
        (indicator_name, business_definition, formula, data_source, update_frequency,
         owner, scope, yellow_threshold, red_threshold, version_date)
      VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
      ON CONFLICT (indicator_name) DO UPDATE SET
        business_definition = EXCLUDED.business_definition,
        formula = EXCLUDED.formula,
        data_source = EXCLUDED.data_source,
        update_frequency = EXCLUDED.update_frequency,
        owner = EXCLUDED.owner,
        scope = EXCLUDED.scope,
        yellow_threshold = EXCLUDED.yellow_threshold,
        red_threshold = EXCLUDED.red_threshold,
        version_date = EXCLUDED.version_date
      RETURNING id
    `, [b.indicator_name, b.business_definition, b.formula, b.data_source,
        b.update_frequency, b.owner, b.scope, b.yellow_threshold, b.red_threshold,
        b.version_date || new Date().toISOString().substring(0, 10)])
    sendSuccess(res, { id: result.rows[0].id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/loop-health', async (req: AuthRequest, res) => {
  try {
    const result = await query(`SELECT * FROM analytics.mv_loop_health`)
    sendSuccess(res, result.rows[0] || {
      task_generation_rate: 0,
      store_execution_rate: 0,
      weekly_check_rate: 0,
      monthly_review_rate: 0,
      practice_promotion_rate: 0,
    })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/notifications/dispatch', async (req: AuthRequest, res) => {
  try {
    const result = await query(`SELECT * FROM analytics.f_dispatch_daily_notifications()`)
    sendSuccess(res, result.rows[0] || { dispatched: 0 })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/stores/:code/daily-card', async (req: AuthRequest, res) => {
  try {
    const code = req.params.code
    const month = req.query.month as string | undefined
    let storeNameFilter: string
    let params: any[] = []
    if (month) {
      const monthDate = month + '-01'
      const storeInfo = await query(`SELECT store_name FROM analytics.mv_store_risk_rating_monthly WHERE store_code = $1 AND month_start = $2::date LIMIT 1`, [code, monthDate])
      if (storeInfo.rows.length === 0) { sendSuccess(res, { anomalies: [], todos: [] }); return }
      const storeName = storeInfo.rows[0].store_name
      params = [storeName, monthDate]
      storeNameFilter = `c003 = $1 AND c176 IS NOT NULL AND c176 != '' AND c176::timestamp >= $2::date AND c176::timestamp < ($2::date + interval '1 month')`
    } else {
      const storeInfo = await query(`SELECT store_name FROM analytics.mv_store_risk_rating_monthly WHERE store_code = $1 LIMIT 1`, [code])
      if (storeInfo.rows.length === 0) { sendSuccess(res, { anomalies: [], todos: [] }); return }
      const storeName = storeInfo.rows[0].store_name
      params = [storeName]
      storeNameFilter = `c003 = $1 AND c176 IS NOT NULL AND c176 != ''`
    }
    const result = await query(`
      WITH target_day AS (
        SELECT max(c176::date) AS business_date
        FROM bill_records
        WHERE ${storeNameFilter}
      ),
      today_stats AS (
        SELECT c003 AS store_name,
          round(sum(COALESCE(NULLIF(c009,'')::numeric,0)), 2) AS consumption,
          round(sum(COALESCE(NULLIF(c068,'')::numeric,0)), 2) AS discount,
          round(sum(${RECEIVED_COLS}), 2) AS received,
          count(*) AS bill_count,
          round(sum(${RECEIVED_COLS}) / count(*), 2) AS avg_bill
        FROM bill_records bf, target_day td
        WHERE ${storeNameFilter.replace('$1', '$1')} AND bf.c176::date = td.business_date
        GROUP BY c003
      ),
      week_ago_stats AS (
        SELECT
          round(sum(COALESCE(NULLIF(c009,'')::numeric,0)), 2) AS consumption,
          round(sum(COALESCE(NULLIF(c068,'')::numeric,0)), 2) AS discount,
          round(sum(${RECEIVED_COLS}), 2) AS received,
          count(*) AS bill_count,
          round(sum(${RECEIVED_COLS}) / count(*), 2) AS avg_bill
        FROM bill_records bf, target_day td
        WHERE ${storeNameFilter.replace('$1', '$1')} AND bf.c176::date = td.business_date - interval '7 days'
        GROUP BY c003
      )
      SELECT t.store_name, '收入' AS module,
        td.business_date AS target_date,
        (td.business_date - interval '7 days')::date AS baseline_date,
        jsonb_build_array(
          jsonb_build_object('metric', '营业收入', 'value', t.consumption, 'baseline', COALESCE(w.consumption, 0), 'is_anomaly', t.consumption < (COALESCE(w.consumption, 0) * 0.8)),
          jsonb_build_object('metric', '优惠', 'value', t.discount, 'baseline', COALESCE(w.discount, 0), 'is_anomaly', t.discount > (COALESCE(w.discount, 0) * 1.2)),
          jsonb_build_object('metric', '实收', 'value', t.received, 'baseline', COALESCE(w.received, 0), 'is_anomaly', t.received < (COALESCE(w.received, 0) * 0.8)),
          jsonb_build_object('metric', '账单数', 'value', t.bill_count, 'baseline', COALESCE(w.bill_count, 0), 'is_anomaly', t.bill_count::numeric < (COALESCE(w.bill_count, 0) * 0.8)),
          jsonb_build_object('metric', '客单价', 'value', t.avg_bill, 'baseline', COALESCE(w.avg_bill, 0), 'is_anomaly', t.avg_bill < (COALESCE(w.avg_bill, 0) * 0.9))
        ) AS anomalies
      FROM today_stats t, target_day td
      LEFT JOIN week_ago_stats w ON true
    `, params)
    const tasks = await query(`
      SELECT t.* FROM analytics.store_task t
      WHERE t.store_code = $1 AND t.status IN ('待启动', '进行中')
      ORDER BY t.priority LIMIT 3
    `, [code])
    const targetDate = result.rows[0]?.target_date
    const baselineDate = result.rows[0]?.baseline_date
    sendSuccess(res, { anomalies: result.rows, todos: tasks.rows, target_date: targetDate, baseline_date: baselineDate })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

// ============================================================
// 动态路径路由 /:id（必须在所有固定路径之后）
// ============================================================

router.get('/:id', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    if (isNaN(id)) {
      return sendError(res, 'Invalid task id', 400)
    }
    const result = await query(`SELECT * FROM analytics.store_task WHERE task_id = $1`, [id])
    if (result.rows.length === 0) {
      return sendError(res, 'Task not found', 404)
    }
    const logs = await query(`SELECT * FROM analytics.store_task_log WHERE task_id = $1 ORDER BY created_at`, [id])
    sendSuccess(res, { task: result.rows[0], logs: logs.rows })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.put('/:id', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const b = req.body
    const fields = ['status', 'process_evidence', 'verification_result', 'incomplete_reason', 'next_step', 'owner', 'collaborators', 'deadline', 'action_required']
    const updates: string[] = []
    const params: any[] = []
    let idx = 1
    for (const f of fields) {
      if (b[f] !== undefined) {
        updates.push(`${f} = $${idx++}`)
        params.push(b[f])
      }
    }
    if (updates.length === 0) {
      return sendError(res, 'No fields to update')
    }
    updates.push(`updated_at = NOW()`)
    params.push(id)
    await query(`UPDATE analytics.store_task SET ${updates.join(', ')} WHERE task_id = $${idx}`, params)
    await query(`
      INSERT INTO analytics.store_task_log (task_id, action, new_status, operator, comment)
      VALUES ($1, '更新', $2, $3, $4)
    `, [id, b.status || null, req.user?.name || 'system', b.next_step || null])
    sendSuccess(res, { task_id: id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.put('/:id/execute', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const b = req.body
    await withTransaction(async (client) => {
      await client.query(`
        UPDATE analytics.store_task
        SET status = '进行中', process_evidence = $1, action_required = $2,
            incomplete_reason = $3, next_step = $4, updated_at = NOW()
        WHERE task_id = $5
      `, [b.process_evidence, b.action_required, b.incomplete_reason, b.next_step, id])
      await client.query(`
        INSERT INTO analytics.store_task_log (task_id, action, old_status, new_status, operator, comment)
        VALUES ($1, '执行反馈', '待启动', '进行中', $2, $3)
      `, [id, req.user?.name || 'store', b.process_evidence])
    })
    sendSuccess(res, { task_id: id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.put('/:id/weekly-check', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const b = req.body
    await withTransaction(async (client) => {
      await client.query(`
        UPDATE analytics.store_task
        SET process_evidence = COALESCE($1, process_evidence),
            next_step = $2, updated_at = NOW()
        WHERE task_id = $3
      `, [b.check_evidence, b.next_step, id])

      await client.query(`
        INSERT INTO analytics.store_task_log (task_id, action, new_status, operator, comment)
        VALUES ($1, '周度检查', NULL, $2, $3)
      `, [id, req.user?.name || 'regional', b.check_comment])

      const task = await client.query(`SELECT store_code, store_name, problem_indicator, plan_month FROM analytics.store_task WHERE task_id = $1`, [id])
      if (task.rows.length > 0) {
        const t = task.rows[0]
        await client.query(`
          INSERT INTO analytics.task_weekly_check
            (task_id, store_code, store_name, problem_indicator, iso_week,
             this_week_value, last_week_value, change_direction,
             consecutive_no_improve_weeks, check_comment, checked_by)
          VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
        `, [id, t.store_code, t.store_name, t.problem_indicator, b.iso_week,
            b.this_week_value, b.last_week_value, b.change_direction,
            b.consecutive_no_improve_weeks || 0, b.check_comment, req.user?.name])
      }
    })
    sendSuccess(res, { task_id: id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.put('/:id/verify', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const b = req.body
    const reviewResult = b.review_result || '改善中'
    await withTransaction(async (client) => {
      await client.query(`
        UPDATE analytics.store_task
        SET status = CASE WHEN $1 = '达标' THEN '已验收'
                          WHEN $1 = '未改善' THEN '已验收'
                          ELSE '进行中' END,
            verification_result = $1,
            verification_indicator = $2,
            incomplete_reason = $3,
            next_step = $4,
            updated_at = NOW()
        WHERE task_id = $5
      `, [reviewResult, b.verification_indicator, b.incomplete_reason, b.next_step, id])

      await client.query(`
        INSERT INTO analytics.store_task_log (task_id, action, new_status, operator, comment)
        VALUES ($1, '月度验收', $2, $3, $4)
      `, [id, reviewResult, req.user?.name || 'hq', JSON.stringify({
        revenue_stable: b.revenue_stable,
        margin_improved: b.margin_improved,
        customer_stable: b.customer_stable,
        anomaly_decreased: b.anomaly_decreased,
      })])

      const task = await client.query(`SELECT store_code, store_name, problem_indicator, plan_month, baseline_value, target_value FROM analytics.store_task WHERE task_id = $1`, [id])
      if (task.rows.length > 0) {
        const t = task.rows[0]
        await client.query(`
          INSERT INTO analytics.task_monthly_review
            (task_id, store_code, store_name, plan_month, problem_indicator,
             baseline_value, target_value, actual_value, review_result,
             revenue_stable, margin_improved, customer_stable, anomaly_decreased)
          VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
        `, [id, t.store_code, t.store_name, t.plan_month, t.problem_indicator,
            t.baseline_value, t.target_value, b.actual_value, reviewResult,
            b.revenue_stable || false, b.margin_improved || false,
            b.customer_stable || false, b.anomaly_decreased || false])
      }
    })
    sendSuccess(res, { task_id: id, review_result: reviewResult })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.post('/:id/rollback', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const reason = req.body.reason
    await withTransaction(async (client) => {
      await client.query(`
        UPDATE analytics.store_task SET status = '已回滚', incomplete_reason = $1, updated_at = NOW()
        WHERE task_id = $2
      `, [reason, id])
      await client.query(`
        INSERT INTO analytics.store_task_log (task_id, action, new_status, operator, comment)
        VALUES ($1, '回滚', '已回滚', $2, $3)
      `, [id, req.user?.name || 'hq', reason])
    })
    sendSuccess(res, { task_id: id })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

// ============================================================
// 本体标准 (Ontology Standard)
// ============================================================

router.get('/ontology/overview', async (req: AuthRequest, res) => {
  try {
    const dims = await query(`
      SELECT table_name, 
        (SELECT count(*) FROM information_schema.columns WHERE table_schema='analytics' AND table_name=t.table_name) AS column_count,
        (SELECT reltuples::bigint FROM pg_class WHERE relname=t.table_name) AS row_count
      FROM information_schema.tables t
      WHERE t.table_schema='analytics' AND t.table_name LIKE 'dim\_%'
      ORDER BY t.table_name
    `)
    const facts = await query(`
      SELECT table_name,
        (SELECT count(*) FROM information_schema.columns WHERE table_schema='analytics' AND table_name=t.table_name) AS column_count,
        (SELECT reltuples::bigint FROM pg_class WHERE relname=t.table_name) AS row_count
      FROM information_schema.tables t
      WHERE t.table_schema='analytics' AND t.table_name LIKE 'fact\_%'
      ORDER BY t.table_name
    `)
    const enums = await query(`
      SELECT table_name,
        (SELECT count(*) FROM information_schema.columns WHERE table_schema='analytics' AND table_name=t.table_name) AS column_count,
        (SELECT reltuples::bigint FROM pg_class WHERE relname=t.table_name) AS row_count
      FROM information_schema.tables t
      WHERE t.table_schema='analytics' AND t.table_name LIKE 'enum\_%'
      ORDER BY t.table_name
    `)
    const metrics = await query(`
      SELECT count(*) AS total, 
        count(*) FILTER (WHERE metric_category IS NOT NULL) AS standardized,
        count(*) FILTER (WHERE is_active = true) AS active
      FROM analytics.indicator_dictionary
    `)
    sendSuccess(res, {
      dimensions: dims.rows,
      facts: facts.rows,
      enums: enums.rows,
      metrics: metrics.rows[0],
    })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/ontology/dim/:table', async (req: AuthRequest, res) => {
  try {
    const tableName = req.params.table
    if (!/^dim_\w+$/.test(tableName)) { sendError(res, 'Invalid table name'); return }
    const cols = await query(`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns
      WHERE table_schema='analytics' AND table_name=$1
      ORDER BY ordinal_position
    `, [tableName])
    const rows = await query(`SELECT * FROM analytics.${tableName} LIMIT 100`)
    sendSuccess(res, { columns: cols.rows, rows: rows.rows })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/ontology/fact/:table', async (req: AuthRequest, res) => {
  try {
    const tableName = req.params.table
    if (!/^fact_\w+$/.test(tableName)) { sendError(res, 'Invalid table name'); return }
    const cols = await query(`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns
      WHERE table_schema='analytics' AND table_name=$1
      ORDER BY ordinal_position
    `, [tableName])
    const count = await query(`SELECT count(*) AS cnt FROM analytics.${tableName}`)
    sendSuccess(res, { columns: cols.rows, total: count.rows[0].cnt })
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/ontology/enum/:table', async (req: AuthRequest, res) => {
  try {
    const tableName = req.params.table
    if (!/^enum_\w+$/.test(tableName)) { sendError(res, 'Invalid table name'); return }
    const rows = await query(`SELECT * FROM analytics.${tableName} ORDER BY sort_order`)
    sendSuccess(res, rows.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

router.get('/ontology/metrics', async (req: AuthRequest, res) => {
  try {
    const result = await query(`
      SELECT * FROM analytics.indicator_dictionary 
      ORDER BY metric_category NULLS LAST, indicator_name
    `)
    sendSuccess(res, result.rows)
  } catch (err: any) {
    sendError(res, err.message)
  }
})

// ============================================================
// 月度复盘 API (Phase 10)
// ============================================================

router.get('/monthly-review/completion', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`
      SELECT 
        priority,
        count(*) as total,
        count(*) FILTER (WHERE status = '已验收' OR status = '已回滚') as completed,
        count(*) FILTER (WHERE verification_result = '达标') as passed,
        count(*) FILTER (WHERE verification_result = '改善中') as improving,
        count(*) FILTER (WHERE verification_result = '未改善') as failed,
        ROUND(count(*) FILTER (WHERE status = '已验收' OR status = '已回滚') * 100.0 / NULLIF(count(*), 0), 1) as completion_rate
      FROM analytics.store_task 
      WHERE plan_month = $1 AND (is_simulated = false OR is_simulated IS NULL)
      GROUP BY priority
      ORDER BY priority
    `, [month])
    sendSuccess(res, result.rows)
  } catch (err: any) { sendError(res, err.message) }
})

router.get('/monthly-review/activity-list', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`
      SELECT marketing_plan, bill_count, consumption, discounts, received,
        avg_bill_value, discount_rate_pct, theoretical_margin_pct,
        ROUND(discounts * 100.0 / NULLIF(consumption, 0), 2) as discount_cost_rate,
        ROUND(received - discounts, 2) as net_contribution
      FROM (
        SELECT marketing_plan,
          count(*) AS bill_count,
          sum(consumption) AS consumption,
          sum(discount_total) AS discounts,
          sum(received_total) AS received,
          round(avg(received_total), 2) AS avg_bill_value,
          round(sum(discount_total) / NULLIF(sum(consumption), 0) * 100, 2) AS discount_rate_pct,
          round(sum(theoretical_profit) / NULLIF(sum(received_total), 0) * 100, 2) AS theoretical_margin_pct
        FROM analytics.bill_fact
        WHERE marketing_plan IS NOT NULL
          AND closed_at >= $1::date AND closed_at < ($1::date + interval '1 month')
        GROUP BY marketing_plan
      ) t
      ORDER BY received DESC
    `, [month])
    sendSuccess(res, result.rows)
  } catch (err: any) { sendError(res, err.message) }
})

router.get('/monthly-review/sku-governance', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const abcResult = await query(`
      SELECT abc_class, count(*) as sku_count, 
        ROUND(sum(revenue_share_pct), 1) as total_revenue_share,
        ROUND(avg(revenue_share_pct), 2) as avg_revenue_share
      FROM analytics.mv_dish_sku_abc_monthly
      WHERE month_start = $1
      GROUP BY abc_class 
      ORDER BY abc_class
    `, [month])
    const longtailResult = await query(`
      SELECT dish_name, category_level1, bill_count, sales_quantity, 
        received_amount, revenue_share_pct, cumulative_revenue_share
      FROM analytics.mv_dish_sku_abc_monthly
      WHERE month_start = $1 AND abc_class = 'C-长尾'
      ORDER BY received_amount ASC
      LIMIT 50
    `, [month])
    sendSuccess(res, { summary: abcResult.rows, longtail: longtailResult.rows })
  } catch (err: any) { sendError(res, err.message) }
})

router.get('/monthly-review/indicator-effectiveness', async (req: AuthRequest, res) => {
  try {
    const month = parseMonth(req)
    const result = await query(`
      SELECT t.problem_indicator,
        count(*) as task_count,
        count(*) FILTER (WHERE t.verification_result = '达标') as passed,
        count(*) FILTER (WHERE t.verification_result = '改善中') as improving,
        count(*) FILTER (WHERE t.verification_result = '未改善') as failed,
        ROUND(count(*) FILTER (WHERE t.verification_result = '达标') * 100.0 / NULLIF(count(*), 0), 1) as pass_rate,
        bool_and(t.current_value IS NOT NULL) as has_actual
      FROM analytics.store_task t
      WHERE t.plan_month = $1::date
      GROUP BY t.problem_indicator
      ORDER BY pass_rate DESC NULLS LAST
    `, [month])
    sendSuccess(res, result.rows)
  } catch (err: any) { sendError(res, err.message) }
})

router.put('/indicators/:id/threshold', async (req: AuthRequest, res) => {
  try {
    const id = parseInt(req.params.id)
    const { yellow_threshold, red_threshold } = req.body
    const result = await query(`
      UPDATE analytics.indicator_dictionary 
      SET yellow_threshold = $1, red_threshold = $2, version_date = CURRENT_DATE
      WHERE id = $3 RETURNING *
    `, [yellow_threshold, red_threshold, id])
    if (result.rows.length === 0) return sendError(res, 'Indicator not found', 404)
    await query(`
      INSERT INTO analytics.metric_version_log (indicator_id, indicator_name, change_type, old_value, new_value, change_reason)
      SELECT $1, indicator_name, 'threshold_adjust', 
        concat('yellow=', COALESCE(yellow_threshold::text, 'NULL'), ', red=', COALESCE(red_threshold::text, 'NULL')),
        concat('yellow=', COALESCE($4::text, 'NULL'), ', red=', COALESCE($5::text, 'NULL')),
        '月度复盘阈值调整'
      FROM analytics.indicator_dictionary WHERE id = $1
    `, [id, yellow_threshold, red_threshold, yellow_threshold, red_threshold])
    sendSuccess(res, result.rows[0])
  } catch (err: any) { sendError(res, err.message) }
})

export default router
