-- Migration 174: Supplier invoice line handling fields
-- Created: 2026-04-13

ALTER TABLE supplier_invoice_lines
ADD COLUMN IF NOT EXISTS handling_type VARCHAR(40),
ADD COLUMN IF NOT EXISTS target_customer_id INTEGER REFERENCES customers(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS target_sag_id INTEGER REFERENCES sag_sager(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS target_employee_id INTEGER REFERENCES users(user_id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS requires_serial BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS serial_number VARCHAR(120),
ADD COLUMN IF NOT EXISTS asset_id INTEGER REFERENCES hardware_assets(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS resale_ready BOOLEAN NOT NULL DEFAULT FALSE;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'chk_supplier_invoice_lines_handling_type'
    ) THEN
        ALTER TABLE supplier_invoice_lines
        ADD CONSTRAINT chk_supplier_invoice_lines_handling_type
        CHECK (
            handling_type IS NULL
            OR handling_type IN (
                'fakturer_videre',
                'asset',
                'intern_brug',
                'projekt_omkostning',
                'lager',
                'retur_reklamation'
            )
        );
    END IF;
END $$;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'chk_supplier_invoice_lines_serial_requirement'
    ) THEN
        ALTER TABLE supplier_invoice_lines
        ADD CONSTRAINT chk_supplier_invoice_lines_serial_requirement
        CHECK (
            requires_serial = FALSE
            OR (serial_number IS NOT NULL AND btrim(serial_number) <> '')
        );
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_handling_type
ON supplier_invoice_lines(handling_type)
WHERE handling_type IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_target_customer_id
ON supplier_invoice_lines(target_customer_id)
WHERE target_customer_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_target_sag_id
ON supplier_invoice_lines(target_sag_id)
WHERE target_sag_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_target_employee_id
ON supplier_invoice_lines(target_employee_id)
WHERE target_employee_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_asset_id
ON supplier_invoice_lines(asset_id)
WHERE asset_id IS NOT NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_resale_ready
ON supplier_invoice_lines(resale_ready)
WHERE resale_ready = TRUE;

CREATE INDEX IF NOT EXISTS idx_supplier_invoice_lines_serial_number
ON supplier_invoice_lines(serial_number)
WHERE serial_number IS NOT NULL;

COMMENT ON COLUMN supplier_invoice_lines.handling_type IS 'Line handling: fakturer_videre, asset, intern_brug, projekt_omkostning, lager, retur_reklamation';
COMMENT ON COLUMN supplier_invoice_lines.target_customer_id IS 'Customer target for line allocation';
COMMENT ON COLUMN supplier_invoice_lines.target_sag_id IS 'Case target for line allocation';
COMMENT ON COLUMN supplier_invoice_lines.target_employee_id IS 'Employee target for line allocation';
COMMENT ON COLUMN supplier_invoice_lines.requires_serial IS 'Whether serial number is required';
COMMENT ON COLUMN supplier_invoice_lines.serial_number IS 'Serial number when required';
COMMENT ON COLUMN supplier_invoice_lines.asset_id IS 'Linked hardware asset id';
COMMENT ON COLUMN supplier_invoice_lines.resale_ready IS 'Whether line is ready for resale flow';
