-- WMS-PICK-001 Golden Screen reference schema.
create schema if not exists wms;

create table if not exists wms.picking_tasks (
    id uuid primary key,
    task_no varchar(80) not null unique,
    status varchar(30) not null check (status in ('READY','IN_PROGRESS','BLOCKED','COMPLETED','CANCELLED')),
    assigned_to varchar(200),
    version bigint not null default 1 check (version > 0),
    started_at timestamptz,
    completed_at timestamptz,
    created_at timestamptz not null default now(),
    updated_at timestamptz
);

create index if not exists ix_picking_tasks_status on wms.picking_tasks(status, created_at);

create table if not exists wms.picking_lines (
    id uuid primary key,
    task_id uuid not null references wms.picking_tasks(id),
    line_no integer not null check (line_no > 0),
    location_code varchar(80) not null,
    location_barcode varchar(120) not null,
    location_confirmed boolean not null default false,
    item_id uuid not null references catalog.items(id),
    item_option varchar(200),
    barcode varchar(120) not null,
    required_qty numeric(18,4) not null check (required_qty > 0),
    picked_qty numeric(18,4) not null default 0 check (picked_qty >= 0 and picked_qty <= required_qty),
    status varchar(30) not null default 'READY' check (status in ('READY','IN_PROGRESS','COMPLETED','BLOCKED')),
    completed_at timestamptz,
    updated_at timestamptz,
    unique(task_id, line_no)
);

create index if not exists ix_picking_lines_task_status on wms.picking_lines(task_id, status, line_no);
create index if not exists ix_picking_lines_barcode on wms.picking_lines(barcode);

-- Stores the exact response for each scanner command so network retries are safe and deterministic.
create table if not exists wms.scan_receipts (
    id uuid primary key,
    task_id uuid not null references wms.picking_tasks(id),
    idempotency_key varchar(200) not null,
    barcode varchar(200) not null,
    source varchar(40) not null,
    occurred_at timestamptz not null,
    actor varchar(200) not null,
    response_payload jsonb not null,
    created_at timestamptz not null default now(),
    unique(task_id, idempotency_key)
);

create index if not exists ix_scan_receipts_task_created on wms.scan_receipts(task_id, created_at desc);

create table if not exists wms.picking_exceptions (
    id uuid primary key,
    task_id uuid not null references wms.picking_tasks(id),
    line_id uuid references wms.picking_lines(id),
    exception_type varchar(50) not null,
    memo varchar(300),
    status varchar(30) not null check (status in ('OPEN','RESOLVED','CANCELLED')),
    reported_at timestamptz not null,
    reported_by varchar(200) not null,
    resolved_at timestamptz,
    resolved_by varchar(200),
    idempotency_key varchar(200) not null,
    unique(task_id, idempotency_key)
);

create index if not exists ix_picking_exceptions_open on wms.picking_exceptions(task_id, status) where status='OPEN';

create table if not exists wms.quantity_receipts (
    id uuid primary key,
    task_id uuid not null references wms.picking_tasks(id),
    idempotency_key varchar(200) not null,
    line_id uuid not null references wms.picking_lines(id),
    picked_qty numeric(18,4) not null,
    actor varchar(200) not null,
    response_payload jsonb not null,
    created_at timestamptz not null default now(),
    unique(task_id, idempotency_key)
);
