-- Create production_standards table
CREATE TABLE IF NOT EXISTS production_standards (
    id BIGSERIAL PRIMARY KEY,
    name VARCHAR(100) UNIQUE NOT NULL,
    project_category VARCHAR(20) NOT NULL CHECK (project_category IN ('GROWING', 'LAYING')),
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    deleted_at TIMESTAMPTZ,
    created_by BIGINT
);

-- Create index for deleted_at (soft delete)
CREATE INDEX idx_production_standards_deleted_at ON production_standards(deleted_at);

-- Tambahkan Foreign Key ke users
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
        ALTER TABLE production_standards
            ADD CONSTRAINT fk_production_standards_created_by
            FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE;
    END IF;
END $$;

-- Index
CREATE INDEX idx_production_standards_created_by ON production_standards(created_by);

-- Create production_standard_details table
CREATE TABLE IF NOT EXISTS production_standard_details (
    id BIGSERIAL PRIMARY KEY,
    production_standard_id BIGINT NOT NULL,
    week INT NOT NULL,
    target_hen_day_production NUMERIC(15, 3),
    target_hen_house_production NUMERIC(15, 3),
    target_egg_weight NUMERIC(15, 3),
    target_egg_mass NUMERIC(15, 3),
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Tambahkan Foreign Key ke production_standards
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'production_standards') THEN
        ALTER TABLE production_standard_details
            ADD CONSTRAINT fk_production_standard_details_standard
            FOREIGN KEY (production_standard_id) REFERENCES production_standards(id) ON DELETE CASCADE;
    END IF;
END $$;

-- Create unique constraint for standard_id + week
CREATE UNIQUE INDEX idx_production_standard_details_standard_week
    ON production_standard_details(production_standard_id, week);

-- Create standard_growth_details table
CREATE TABLE IF NOT EXISTS standard_growth_details (
    id BIGSERIAL PRIMARY KEY,
    production_standard_id BIGINT NOT NULL,
    target_mean_bw NUMERIC(15, 3),
    max_depletion NUMERIC(15, 3),
    min_uniformity NUMERIC(15, 3) NOT NULL,
    week INT NOT NULL,
    feed_intake NUMERIC(15, 3),
    created_at TIMESTAMPTZ DEFAULT NOW(),
    created_by BIGINT
);

-- Tambahkan Foreign Key ke production_standards
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'production_standards') THEN
        ALTER TABLE standard_growth_details
            ADD CONSTRAINT fk_standard_growth_details_standard
            FOREIGN KEY (production_standard_id) REFERENCES production_standards(id) ON DELETE CASCADE;
    END IF;
END $$;

-- Tambahkan Foreign Key ke users
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'users') THEN
        ALTER TABLE standard_growth_details
            ADD CONSTRAINT fk_standard_growth_details_created_by
            FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE;
    END IF;
END $$;

-- Create unique constraint for standard_id + week
CREATE UNIQUE INDEX idx_standard_growth_details_standard_week
    ON standard_growth_details(production_standard_id, week);

-- Index
CREATE INDEX idx_standard_growth_details_created_by ON standard_growth_details(created_by);

-- Create index for project_category
CREATE INDEX idx_production_standards_project_category ON production_standards(project_category);
