BEGIN;

-- Remove grading details from recording_eggs
ALTER TABLE recording_eggs
    DROP CONSTRAINT IF EXISTS chk_recording_eggs_qty;

ALTER TABLE recording_eggs
    DROP COLUMN IF EXISTS weight;

ALTER TABLE recording_eggs
    ADD CONSTRAINT chk_recording_eggs_qty CHECK (qty >= 0);

-- Restore grading_eggs table for rollback scenarios
CREATE TABLE grading_eggs (
    id                BIGSERIAL PRIMARY KEY,
    recording_egg_id  BIGINT NOT NULL,
    qty               NUMERIC(15,3) NOT NULL,
    grade             VARCHAR,
    created_by        BIGINT,
    created_at        TIMESTAMPTZ DEFAULT NOW(),
    updated_at        TIMESTAMPTZ DEFAULT NOW(),

    CONSTRAINT fk_grading_eggs_recording_egg
        FOREIGN KEY (recording_egg_id) REFERENCES recording_eggs(id) ON DELETE CASCADE,
    CONSTRAINT fk_grading_eggs_created_by
        FOREIGN KEY (created_by) REFERENCES users(id),
    CONSTRAINT chk_grading_eggs_qty CHECK (qty >= 0)
);

CREATE INDEX idx_grading_eggs_recording_egg
    ON grading_eggs (recording_egg_id);

COMMIT;
