/**
 * Stage Loss: CRUD + Post + Unpost + edge + multi-tenant.
 */
import { login, makeClient } from '../lib/api.js'
import type { CheckResult, Step, Report } from '../lib/report.js'
import { generateEan13 } from '../lib/barcode.js'

type Org = {
  orgId: string; email: string; password: string; token: string
  storeId: string; productId: string; productName: string
  currencyId: string; unitKgId: string; groupId: string; priceTypeRetailId: string
}
type Ctx = {
  apiOnly: boolean
  ts: number
  orgA?: Org
  orgB?: Org
  lossIdA?: string
  initialStock?: number
}
interface StepCtx { ctx: Ctx; step: Step; report: Report }

const TS = Date.now()
function check(step: Step, c: CheckResult) { step.checks.push(c) }
function asString(x: unknown): string {
  if (x == null) return ''
  if (typeof x === 'string') return x
  return JSON.stringify(x)
}

async function bootstrapOrg(suffix: string, phone: string, barcodeIdx: number): Promise<Org> {
  const api = makeClient()
  const email = `stage-loss-${suffix}-${TS}@food-market.local`
  const password = 'StageLoss12345!'
  const orgName = `Loss Org ${suffix} ${TS}`
  const r = await api.post('/api/auth/signup', { email, password, organizationName: orgName, phone, plan: 'start' })
  if (r.status !== 200) throw new Error(`signup ${suffix}: ${r.status} ${JSON.stringify(r.data)}`)
  const sess = await login(email, password)
  const auth = makeClient(sess.accessToken)
  const [stores, units, groups, prices, currencies] = await Promise.all([
    auth.get('/api/catalog/stores'),
    auth.get('/api/catalog/units-of-measure'),
    auth.get('/api/catalog/product-groups'),
    auth.get('/api/catalog/price-types'),
    auth.get('/api/catalog/currencies'),
  ])
  const storeId = stores.data.items.find((s: { isMain: boolean }) => s.isMain).id
  const unitKgId = units.data.items.find((u: { code: string }) => u.code === '166').id
  const groupId = groups.data.items.find((g: { parentId: string | null }) => g.parentId == null).id
  const priceTypeRetailId = prices.data.items.find((p: { isRequired: boolean }) => p.isRequired).id
  const currencyId = currencies.data.items.find((c: { code: string }) => c.code === 'KZT').id
  const prod = await auth.post('/api/catalog/products', {
    name: `Loss Prod ${suffix} ${TS}`, article: `L-${suffix}-${TS}`,
    unitOfMeasureId: unitKgId, vat: 0, vatEnabled: false, productGroupId: groupId,
    barcodes: [{ code: generateEan13(barcodeIdx), type: 1, isPrimary: true }],
    prices: [{ priceTypeId: priceTypeRetailId, amount: 100, currencyId }],
  })
  if (prod.status !== 201) throw new Error(`prod ${suffix}: ${prod.status} ${JSON.stringify(prod.data)}`)
  return {
    orgId: r.data.organizationId, email, password, token: sess.accessToken,
    storeId, productId: prod.data.id, productName: prod.data.name,
    currencyId, unitKgId, groupId, priceTypeRetailId,
  }
}

async function getStock(token: string, storeId: string, productId: string): Promise<number> {
  const r = await makeClient(token).get(`/api/inventory/stock?storeId=${storeId}`)
  const row = (r.data?.items ?? []).find((x: { productId: string }) => x.productId === productId)
  return row?.quantity ?? 0
}

// ---------------------------------------------------------------------------

export async function loss01_setup({ ctx, step, report }: StepCtx) {
  ctx.ts = TS
  ctx.orgA = await bootstrapOrg('a', '+77011120001', 21)
  await new Promise(r => setTimeout(r, 900))
  ctx.orgB = await bootstrapOrg('b', '+77011120002', 22)

  // Заполним остаток на 100 через Enter
  const a = ctx.orgA
  const api = makeClient(a.token)
  const enter = await api.post('/api/inventory/enters', {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    lines: [{ productId: a.productId, quantity: 100, unitCost: 50 }],
  })
  const post = await api.post(`/api/inventory/enters/${enter.data.id}/post`, {})
  check(step, { kind: 'api', description: 'Initial Enter posted', ok: post.status === 204, detail: `${post.status}` })
  ctx.initialStock = await getStock(a.token, a.storeId, a.productId)
  check(step, { kind: 'api', description: 'Initial Stock = 100', ok: ctx.initialStock === 100, detail: `stock=${ctx.initialStock}` })
}

// ---------------------------------------------------------------------------

export async function loss02_create_draft({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const res = await api.post('/api/inventory/losses', {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    reason: 0, // Defect
    notes: 'test loss', lines: [{ productId: a.productId, quantity: 10, unitCost: 50 }],
  })
  check(step, {
    kind: 'api', description: 'POST /api/inventory/losses → 201',
    ok: res.status === 201, detail: `${res.status} ${asString(res.data).slice(0, 200)}`,
  })
  if (res.status !== 201) return
  ctx.lossIdA = res.data.id
  check(step, { kind: 'api', description: 'Total = 10*50 = 500', ok: res.data.total === 500, detail: `total=${res.data.total}` })
  check(step, { kind: 'api', description: 'Status = Draft (0)', ok: res.data.status === 0, detail: `status=${res.data.status}` })
}

// ---------------------------------------------------------------------------

export async function loss03_update_draft({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.lossIdA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const upd = await api.put(`/api/inventory/losses/${ctx.lossIdA}`, {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    reason: 1, // Expired
    notes: 'rebound', lines: [{ productId: a.productId, quantity: 15, unitCost: 50 }],
  })
  check(step, {
    kind: 'api', description: 'PUT loss → 204 (фикс EF8)',
    ok: upd.status === 204, detail: `${upd.status} ${asString(upd.data).slice(0, 200)}`,
  })
  const got = await api.get(`/api/inventory/losses/${ctx.lossIdA}`)
  check(step, {
    kind: 'api', description: 'После PUT: total=750, reason=1, lines.length=1',
    ok: got.data?.total === 750 && got.data?.reason === 1 && got.data?.lines?.length === 1,
    detail: `total=${got.data?.total} reason=${got.data?.reason} lines=${got.data?.lines?.length}`,
  })
}

// ---------------------------------------------------------------------------

export async function loss04_post({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.lossIdA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const before = await getStock(a.token, a.storeId, a.productId)
  const post = await api.post(`/api/inventory/losses/${ctx.lossIdA}/post`, {})
  check(step, { kind: 'api', description: 'POST /post → 204', ok: post.status === 204, detail: `${post.status} ${asString(post.data).slice(0, 200)}` })
  const after = await getStock(a.token, a.storeId, a.productId)
  check(step, { kind: 'api', description: 'Stock -= 15', ok: before - after === 15, detail: `before=${before} after=${after}` })

  const movs = await api.get(`/api/inventory/movements?storeId=${a.storeId}&take=200`)
  const ours = (movs.data?.items ?? []).filter((m: { documentId: string }) => m.documentId === ctx.lossIdA)
  check(step, {
    kind: 'api', description: 'StockMovement type=WriteOff (MovementType.WriteOff) создан',
    ok: ours.length >= 1 && (ours[0].type === 'WriteOff' || ours[0].type === 'Loss'),
    detail: `count=${ours.length} type=${ours[0]?.type}`,
  })
  const got = await api.get(`/api/inventory/losses/${ctx.lossIdA}`)
  check(step, {
    kind: 'api', description: 'После Post: Status = Posted (1), PostedAt!=null',
    ok: got.data?.status === 1 && got.data?.postedAt != null,
    detail: `status=${got.data?.status} postedAt=${got.data?.postedAt}`,
  })
}

// ---------------------------------------------------------------------------

export async function loss05_post_more_than_stock({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const current = await getStock(a.token, a.storeId, a.productId)
  // current сейчас = 85 (100 − 15). Попробуем списать 1000 — должно упасть.
  const create = await api.post('/api/inventory/losses', {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    reason: 0, notes: 'over-stock', lines: [{ productId: a.productId, quantity: current + 100, unitCost: 50 }],
  })
  if (create.status !== 201) {
    step.notes.push(`Create unexpected: ${create.status} ${asString(create.data).slice(0, 200)}`)
    return
  }
  const post = await api.post(`/api/inventory/losses/${create.data.id}/post`, {})
  check(step, {
    kind: 'api', description: 'Списание > остатка → 400/409 с понятной ошибкой',
    ok: (post.status === 400 || post.status === 409) && /остат|минус|stock|нал|списать.*больше/i.test(asString(post.data)),
    detail: `${post.status} ${asString(post.data).slice(0, 200)}`,
  })
  if (![400, 409].includes(post.status)) {
    report.bug({
      step: 'loss05', severity: 'critical',
      title: 'Списание больше остатка не блокируется → Stock уходит в минус',
      detail: `status=${post.status}, body=${asString(post.data).slice(0, 200)}`,
    })
  }
}

// ---------------------------------------------------------------------------

export async function loss06_unpost({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.lossIdA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const before = await getStock(a.token, a.storeId, a.productId)
  const unpost = await api.post(`/api/inventory/losses/${ctx.lossIdA}/unpost`, {})
  check(step, { kind: 'api', description: 'POST /unpost → 204', ok: unpost.status === 204, detail: `${unpost.status}` })
  const after = await getStock(a.token, a.storeId, a.productId)
  check(step, { kind: 'api', description: 'Stock += 15 обратно', ok: after - before === 15, detail: `before=${before} after=${after}` })
  const got = await api.get(`/api/inventory/losses/${ctx.lossIdA}`)
  check(step, {
    kind: 'api', description: 'После Unpost: Status = Draft (0)',
    ok: got.data?.status === 0 && got.data?.postedAt == null,
    detail: `status=${got.data?.status} postedAt=${got.data?.postedAt}`,
  })
}

// ---------------------------------------------------------------------------

export async function loss07_reason_invalid({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)

  // Невалидный enum (модель ASP.NET преобразует строки -> enum, но 99999 для int — допустимо.
  // Реальная проверка: out-of-range int останется int с этим значением — domain semantics
  // не определены. Проверяем что хотя бы валидный enum проходит, а явно мусорный — отвергается.
  const bad = await api.post('/api/inventory/losses', {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    reason: 'not-a-number', // строка — JsonConverter должен отвергнуть → 400
    lines: [{ productId: a.productId, quantity: 1, unitCost: 1 }],
  })
  check(step, {
    kind: 'api', description: 'Строка вместо reason → 400 (binding error)',
    ok: bad.status === 400, detail: `${bad.status}`,
  })

  // Reason=99 (Other) — валидный
  const ok = await api.post('/api/inventory/losses', {
    date: new Date().toISOString(), storeId: a.storeId, currencyId: a.currencyId,
    reason: 99, lines: [{ productId: a.productId, quantity: 1, unitCost: 1 }],
  })
  check(step, {
    kind: 'api', description: 'reason=99 (Other) → 201',
    ok: ok.status === 201, detail: `${ok.status} ${asString(ok.data).slice(0, 200)}`,
  })
}

// ---------------------------------------------------------------------------

export async function loss08_multi_tenant_isolation({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.orgB || !ctx.lossIdA) { step.status = 'skip'; return }
  const apiB = makeClient(ctx.orgB.token)
  const list = await apiB.get('/api/inventory/losses?pageSize=200')
  const sees = (list.data?.items ?? []).some((x: { id: string }) => x.id === ctx.lossIdA)
  check(step, {
    kind: 'api', description: 'Org B не видит loss org A в списке',
    ok: !sees, detail: `total=${list.data?.total} sees=${sees}`,
  })
  if (sees) report.bug({ step: 'loss08', severity: 'critical', title: 'P0: loss org A виден из B', detail: `lossId=${ctx.lossIdA}` })
  const get = await apiB.get(`/api/inventory/losses/${ctx.lossIdA}`)
  check(step, { kind: 'api', description: 'GET чужого id → 404', ok: get.status === 404, detail: `${get.status}` })
  const post = await apiB.post(`/api/inventory/losses/${ctx.lossIdA}/post`, {})
  check(step, { kind: 'api', description: 'POST /post чужого → 404', ok: post.status === 404, detail: `${post.status}` })
}
