// DELETE /v1/products/:id  — GUARDED. Deletes one product or 404.
//
// Same scope guard as create. `DELETE` is one of the idempotent methods the
// `Idempotency-Key` dedup covers (app.config `idempotency: true`).

import { defineRestRoute, requireScope } from '@voltro/protocol/rest'
import { Schema } from 'effect'
import type { DataStore } from '@voltro/runtime'

export default defineRestRoute({
  method: 'DELETE',
  path:   '/v1/products/:id',
  input: Schema.Struct({
    params: Schema.Struct({ id: Schema.NonEmptyString }),
  }),
  output:  Schema.Struct({ id: Schema.String, deleted: Schema.Boolean }),
  summary: 'Delete a product by id',
  guards:  [requireScope('products:write')],
  handler: async ({ params }, ctx) => {
    const store = ctx.store as DataStore
    const ok = await store.delete('products', params.id)
    if (!ok) throw { status: 404, message: `product '${params.id}' not found` }
    return { id: params.id, deleted: true }
  },
})
