-- Destructive rollback draft for 005_arr_processing_ingestion.sql.
-- Do not execute after any ARR processing job or v3 daily version has been stored.

BEGIN;

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM finance.processing_jobs)
       OR EXISTS (SELECT 1 FROM finance.processing_attempts)
       OR EXISTS (SELECT 1 FROM finance.processing_callbacks)
       OR EXISTS (SELECT 1 FROM finance.outbox_events)
       OR EXISTS (
           SELECT 1
           FROM finance.daily_versions
           WHERE structured_result_file_id IS NOT NULL
              OR processing_job_id IS NOT NULL
       )
       OR EXISTS (
           SELECT 1
           FROM finance.daily_channel_metrics
           WHERE worksheet_order IS NOT NULL
       )
       OR EXISTS (
           SELECT 1
           FROM booking.file_objects
           WHERE file_kind = 'structured_result_json'
       ) THEN
        RAISE EXCEPTION
            'refusing rollback: migration 005 data exists';
    END IF;
END;
$$;

DROP INDEX finance.daily_channel_metrics_order_unique;

ALTER TABLE finance.daily_channel_metrics
    DROP CONSTRAINT daily_channel_metrics_order_valid,
    DROP COLUMN worksheet_order;

ALTER TABLE finance.daily_records
    DROP CONSTRAINT daily_records_people_valid,
    DROP CONSTRAINT daily_records_room_count_valid,
    DROP CONSTRAINT daily_records_dates_and_nights_valid,
    DROP CONSTRAINT daily_records_total_valid,
    DROP CONSTRAINT daily_records_kb_valid;

ALTER TABLE finance.daily_records
    ADD CONSTRAINT daily_records_people_valid CHECK (
        (adults IS NULL OR adults >= 0)
        AND (children IS NULL OR children >= 0)
    ),
    ADD CONSTRAINT daily_records_room_count_valid CHECK (
        no_of_rooms IS NULL OR no_of_rooms > 0
    ),
    ADD CONSTRAINT daily_records_dates_and_nights_valid CHECK (
        arrival IS NULL
        OR departure IS NULL
        OR nights IS NULL
        OR (
            departure >= arrival
            AND nights = departure - arrival
        )
    ),
    ADD CONSTRAINT daily_records_total_valid CHECK (
        real_price IS NULL
        OR no_of_rooms IS NULL
        OR nights IS NULL
        OR total_price IS NULL
        OR total_price = real_price * no_of_rooms * nights
    ),
    ADD CONSTRAINT daily_records_kb_valid CHECK (
        kb_amount IS NULL
        OR no_of_rooms IS NULL
        OR kb_amount = no_of_rooms * 100
    );

ALTER TABLE finance.daily_versions
    DROP CONSTRAINT daily_version_active_shape;

ALTER TABLE finance.daily_versions
    ADD CONSTRAINT daily_version_active_shape CHECK (
        version_status <> 'active'
        OR (
            business_date IS NOT NULL
            AND validated_at IS NOT NULL
            AND activated_at IS NOT NULL
            AND failure_code IS NULL
            AND (
                (
                    ingestion_mode = 'opera_xml'
                    AND daily_report_file_id IS NOT NULL
                    AND result_json_file_id IS NOT NULL
                )
                OR ingestion_mode = 'monthly_backfill'
            )
        )
    );

DROP INDEX finance.daily_versions_processing_job_unique;
DROP INDEX finance.daily_versions_source_date_rule_unique;

CREATE UNIQUE INDEX daily_versions_source_date_mode_unique
    ON finance.daily_versions (
        source_file_id,
        business_date,
        ingestion_mode
    );

ALTER TABLE finance.daily_versions
    DROP COLUMN structured_result_file_id,
    DROP COLUMN processing_job_id;

DROP TABLE finance.outbox_events;
DROP TABLE finance.processing_callbacks;
DROP TABLE finance.processing_attempts;
DROP TABLE finance.processing_jobs;

ALTER TABLE booking.file_objects
    DROP CONSTRAINT file_objects_file_kind_check;

ALTER TABLE booking.file_objects
    ADD CONSTRAINT file_objects_file_kind_check CHECK (file_kind IN (
        'booking_excel',
        'opera_xml',
        'daily_xlsx',
        'monthly_xlsx',
        'company_ten_day_xlsx',
        'exception_xlsx',
        'result_json'
    ));

COMMIT;
