/**
 * Stage Demand: оптовая отгрузка. 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; customerId: string
  currencyId: string; unitKgId: string; groupId: string; priceTypeRetailId: string
}
type Ctx = {
  apiOnly: boolean
  ts: number
  orgA?: Org
  orgB?: Org
  demandIdA?: string
  creditDemandIdA?: string
}
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, bcIdx: number): Promise<Org> {
  const api = makeClient()
  const email = `stage-dm-${suffix}-${TS}@food-market.local`
  const password = 'StageDm12345!'
  let r = await api.post('/api/auth/signup', { email, password, organizationName: `DM ${suffix} ${TS}`, phone, plan: 'start' })
  for (let i = 0; i < 5 && r.status === 429; i++) {
    await new Promise(res => setTimeout(res, 15000))
    r = await api.post('/api/auth/signup', { email, password, organizationName: `DM ${suffix} ${TS}`, 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 customer = await auth.post('/api/catalog/counterparties', {
    name: `ТОО Покупатель ${suffix} ${TS}`, legalName: `ТОО Покупатель ${suffix} ${TS}`, type: 1,
    bin: `12345699999${bcIdx % 10}`, iin: null, taxNumber: null,
    countryId: null, address: null, phone: '+77011112233', email: null,
    bankName: null, bankAccount: null, bik: null, contactPerson: null, notes: null,
  })
  if (customer.status !== 201) throw new Error(`customer ${suffix}: ${customer.status} ${JSON.stringify(customer.data)}`)

  const prod = await auth.post('/api/catalog/products', {
    name: `DM Prod ${suffix} ${TS}`, article: `DM-${suffix}-${TS}`,
    unitOfMeasureId: unitKgId, vat: 0, vatEnabled: false, productGroupId: groupId,
    barcodes: [{ code: generateEan13(bcIdx), type: 1, isPrimary: true }],
    prices: [{ priceTypeId: priceTypeRetailId, amount: 500, currencyId }],
  })
  if (prod.status !== 201) throw new Error(`prod ${suffix}: ${prod.status} ${JSON.stringify(prod.data)}`)

  // Initial stock = 100 через Enter
  const enter = await auth.post('/api/inventory/enters', {
    date: new Date().toISOString(), storeId, currencyId,
    lines: [{ productId: prod.data.id, quantity: 100, unitCost: 300 }],
  })
  await auth.post(`/api/inventory/enters/${enter.data.id}/post`, {})

  return {
    orgId: r.data.organizationId, email, password, token: sess.accessToken,
    storeId, productId: prod.data.id, customerId: customer.data.id,
    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 dm01_setup({ ctx, step, report }: StepCtx) {
  ctx.ts = TS
  ctx.orgA = await bootstrapOrg('a', '+77011170001', 71)
  await new Promise(r => setTimeout(r, 1500))
  ctx.orgB = await bootstrapOrg('b', '+77011170002', 72)
  const stock = await getStock(ctx.orgA.token, ctx.orgA.storeId, ctx.orgA.productId)
  check(step, { kind: 'api', description: 'Initial Stock = 100', ok: stock === 100, detail: `${stock}` })
}

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

export async function dm02_create_draft_full_paid({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  // PaymentMethod.Cash=0, qty=5 × unitPrice=400 = 2000
  const res = await api.post('/api/sales/demands', {
    date: new Date().toISOString(),
    customerId: a.customerId, storeId: a.storeId, currencyId: a.currencyId,
    payment: 0, paidAmount: 2000, notes: 'оптовая Cash',
    lines: [{ productId: a.productId, quantity: 5, unitPrice: 400, discount: 0, vatPercent: 0 }],
  })
  check(step, { kind: 'api', description: 'POST demand → 201', ok: res.status === 201, detail: `${res.status} ${asString(res.data).slice(0, 200)}` })
  if (res.status !== 201) return
  ctx.demandIdA = res.data.id
  check(step, { kind: 'api', description: 'Total = 2000', ok: res.data.total === 2000, detail: `total=${res.data.total}` })
  check(step, { kind: 'api', description: 'PaidAmount = 2000', ok: res.data.paidAmount === 2000, detail: `paid=${res.data.paidAmount}` })
}

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

export async function dm03_update_draft({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.demandIdA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  const upd = await api.put(`/api/sales/demands/${ctx.demandIdA}`, {
    date: new Date().toISOString(),
    customerId: a.customerId, storeId: a.storeId, currencyId: a.currencyId,
    payment: 0, paidAmount: 3000, notes: 'updated',
    lines: [{ productId: a.productId, quantity: 6, unitPrice: 500, discount: 0, vatPercent: 0 }],
  })
  check(step, { kind: 'api', description: 'PUT demand → 204', ok: upd.status === 204, detail: `${upd.status} ${asString(upd.data).slice(0, 200)}` })
  const got = await api.get(`/api/sales/demands/${ctx.demandIdA}`)
  check(step, { kind: 'api', description: 'После PUT: total=3000, paid=3000', ok: got.data?.total === 3000 && got.data?.paidAmount === 3000, detail: `total=${got.data?.total} paid=${got.data?.paidAmount}` })
}

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

export async function dm04_post_full_paid({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.demandIdA) { 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/sales/demands/${ctx.demandIdA}/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 -= 6', ok: before - after === 6, detail: `${before} → ${after}` })
  const movs = await api.get('/api/inventory/movements?take=200')
  const ours = (movs.data?.items ?? []).filter((m: { documentId: string }) => m.documentId === ctx.demandIdA)
  check(step, {
    kind: 'api', description: 'StockMovement type=WholesaleSale',
    ok: ours.length >= 1 && ours[0].type === 'WholesaleSale',
    detail: `count=${ours.length} type=${ours[0]?.type}`,
  })
}

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

export async function dm05_credit_partial_payment({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA) { step.status = 'skip'; return }
  const a = ctx.orgA
  const api = makeClient(a.token)
  // Payment=Credit (3), qty=4 × 400 = 1600, paid=0 → дебиторка 1600
  const create = await api.post('/api/sales/demands', {
    date: new Date().toISOString(),
    customerId: a.customerId, storeId: a.storeId, currencyId: a.currencyId,
    payment: 3, paidAmount: 0, notes: 'кредит',
    lines: [{ productId: a.productId, quantity: 4, unitPrice: 400, discount: 0, vatPercent: 0 }],
  })
  check(step, { kind: 'api', description: 'Credit draft → 201', ok: create.status === 201, detail: `${create.status} ${asString(create.data).slice(0, 200)}` })
  if (create.status !== 201) return
  ctx.creditDemandIdA = create.data.id
  const post = await api.post(`/api/sales/demands/${ctx.creditDemandIdA}/post`, {})
  check(step, { kind: 'api', description: 'Credit post → 204', ok: post.status === 204, detail: `${post.status} ${asString(post.data).slice(0, 200)}` })
  const got = await api.get(`/api/sales/demands/${ctx.creditDemandIdA}`)
  check(step, {
    kind: 'api', description: 'После Post: payment=Credit(3), total=1600, paid=0 (debt=1600)',
    ok: got.data?.payment === 3 && got.data?.total === 1600 && got.data?.paidAmount === 0,
    detail: `payment=${got.data?.payment} total=${got.data?.total} paid=${got.data?.paidAmount}`,
  })
}

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

export async function dm06_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 avail = await getStock(a.token, a.storeId, a.productId)
  const create = await api.post('/api/sales/demands', {
    date: new Date().toISOString(),
    customerId: a.customerId, storeId: a.storeId, currencyId: a.currencyId,
    payment: 0, paidAmount: 0, notes: 'over-stock',
    lines: [{ productId: a.productId, quantity: avail + 100, unitPrice: 400, discount: 0, vatPercent: 0 }],
  })
  if (create.status !== 201) { step.notes.push(`create unexpected: ${create.status}`); return }
  const post = await api.post(`/api/sales/demands/${create.data.id}/post`, {})
  check(step, {
    kind: 'api', description: 'Post qty > stock → 409',
    ok: post.status === 409 && /остат|stock|больше|недостаточ|нал/i.test(asString(post.data)),
    detail: `${post.status} ${asString(post.data).slice(0, 200)}`,
  })
}

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

export async function dm07_unpost({ ctx, step, report }: StepCtx) {
  if (!ctx.orgA || !ctx.demandIdA) { 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/sales/demands/${ctx.demandIdA}/unpost`, {})
  check(step, { kind: 'api', description: 'POST /unpost → 204', ok: unpost.status === 204, detail: `${unpost.status} ${asString(unpost.data).slice(0, 200)}` })
  const after = await getStock(a.token, a.storeId, a.productId)
  check(step, { kind: 'api', description: 'Stock += 6 обратно', ok: after - before === 6, detail: `${before} → ${after}` })
  const got = await api.get(`/api/sales/demands/${ctx.demandIdA}`)
  check(step, { kind: 'api', description: 'Status = Draft (0)', ok: got.data?.status === 0, detail: `status=${got.data?.status}` })
}

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

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