-- Create laying_transfer_sources dan laying_transfer_targets tables

-- 1. Create laying_transfer_sources table (detail sumber - kandang asal growing)
CREATE TABLE laying_transfer_sources (
    id BIGSERIAL PRIMARY KEY,
    laying_transfer_id BIGINT NOT NULL,
    source_project_flock_kandang_id BIGINT NOT NULL,
    product_warehouse_id BIGINT,
    qty NUMERIC(15, 3) NOT NULL,
    note TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ
);

-- Add foreign keys untuk laying_transfer_sources
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'laying_transfers') THEN
        ALTER TABLE laying_transfer_sources
            ADD CONSTRAINT fk_laying_transfer_sources_laying_transfer_id 
            FOREIGN KEY (laying_transfer_id) REFERENCES laying_transfers(id) ON DELETE CASCADE;
    END IF;

    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
        ALTER TABLE laying_transfer_sources
            ADD CONSTRAINT fk_laying_transfer_sources_project_flock_kandang_id 
            FOREIGN KEY (source_project_flock_kandang_id) REFERENCES project_flock_kandangs(id) ON DELETE RESTRICT;
    END IF;

    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
        ALTER TABLE laying_transfer_sources
            ADD CONSTRAINT fk_laying_transfer_sources_product_warehouse_id 
            FOREIGN KEY (product_warehouse_id) REFERENCES product_warehouses(id) ON DELETE SET NULL;
    END IF;
END $$;

-- 2. Create laying_transfer_targets table (detail tujuan - kandang laying)
CREATE TABLE laying_transfer_targets (
    id BIGSERIAL PRIMARY KEY,
    laying_transfer_id BIGINT NOT NULL,
    target_project_flock_kandang_id BIGINT NOT NULL,
    qty NUMERIC(15, 3) NOT NULL,
    product_warehouse_id BIGINT,
    note TEXT,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ
);

-- Add foreign keys untuk laying_transfer_targets
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'laying_transfers') THEN
        ALTER TABLE laying_transfer_targets
            ADD CONSTRAINT fk_laying_transfer_targets_laying_transfer_id 
            FOREIGN KEY (laying_transfer_id) REFERENCES laying_transfers(id) ON DELETE CASCADE;
    END IF;

    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'project_flock_kandangs') THEN
        ALTER TABLE laying_transfer_targets
            ADD CONSTRAINT fk_laying_transfer_targets_project_flock_kandang_id 
            FOREIGN KEY (target_project_flock_kandang_id) REFERENCES project_flock_kandangs(id) ON DELETE RESTRICT;
    END IF;

    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'product_warehouses') THEN
        ALTER TABLE laying_transfer_targets
            ADD CONSTRAINT fk_laying_transfer_targets_product_warehouse_id 
            FOREIGN KEY (product_warehouse_id) REFERENCES product_warehouses(id) ON DELETE SET NULL;
    END IF;
END $$;

-- 3. Create indexes untuk laying_transfer_sources
CREATE INDEX idx_laying_transfer_sources_laying_transfer_id ON laying_transfer_sources (laying_transfer_id);

CREATE INDEX idx_laying_transfer_sources_source_kandang_id ON laying_transfer_sources (
    source_project_flock_kandang_id
);

CREATE INDEX idx_laying_transfer_sources_product_warehouse_id ON laying_transfer_sources (product_warehouse_id);

CREATE INDEX idx_laying_transfer_sources_deleted_at ON laying_transfer_sources (deleted_at);

-- 4. Create indexes untuk laying_transfer_targets
CREATE INDEX idx_laying_transfer_targets_laying_transfer_id ON laying_transfer_targets (laying_transfer_id);

CREATE INDEX idx_laying_transfer_targets_target_kandang_id ON laying_transfer_targets (
    target_project_flock_kandang_id
);

CREATE INDEX idx_laying_transfer_targets_product_warehouse_id ON laying_transfer_targets (product_warehouse_id);

CREATE INDEX idx_laying_transfer_targets_deleted_at ON laying_transfer_targets (deleted_at);