-- Rounding is lossy, but an integer column cannot hold the fractional periods.
ALTER TABLE enapo_ppas_distribution_invoice_price
	ALTER COLUMN time_slot TYPE int4 USING round(time_slot)::int4;

-- Give up the per-installation scoping. The pre-existing keys cannot represent more than one
-- installation per invoice, so colliding rows beyond the lowest place_id are dropped first -- the same
-- lossy restore the invoice header gets below.
DELETE FROM enapo_ppas_distribution_invoice_device d
USING enapo_ppas_distribution_invoice_device x
WHERE d.device_serial_number = x.device_serial_number
	AND d.invoice_id = x.invoice_id
	AND d.date_from = x.date_from
	AND d.date_to = x.date_to
	AND d.reading_type = x.reading_type
	AND d.kind = x.kind
	AND d.place_id > x.place_id;

DELETE FROM enapo_ppas_distribution_invoice_price p
USING enapo_ppas_distribution_invoice_price x
WHERE p.invoice_id = x.invoice_id
	AND p.customer_id = x.customer_id
	AND p.date_from = x.date_from
	AND p.date_to = x.date_to
	AND p.kind = x.kind
	AND p.price_type = x.price_type
	AND p.place_id > x.place_id;

-- Dropping the column cascades away both the place_id index and the primary key that contains it.
ALTER TABLE enapo_ppas_distribution_invoice_device DROP COLUMN place_id;
ALTER TABLE enapo_ppas_distribution_invoice_device
	ADD CONSTRAINT enapo_ppas_distribution_invoice_device_pk
		PRIMARY KEY (device_serial_number, invoice_id, date_from, date_to, reading_type, kind);

ALTER TABLE enapo_ppas_distribution_invoice_price DROP COLUMN place_id;
ALTER TABLE enapo_ppas_distribution_invoice_price
	ADD CONSTRAINT enapo_ppas_distribution_invoice_price_pk
		PRIMARY KEY (invoice_id, customer_id, date_from, date_to, kind, price_type);

ALTER TABLE enapo_ppas_distribution_invoice
	ADD COLUMN eic varchar NULL,
	ADD COLUMN place_id varchar NULL,
	ADD COLUMN installations_billing_class varchar NULL,
	ADD COLUMN installations_measurement_type varchar NULL,
	ADD COLUMN installations_contract_contract_id varchar NULL,
	ADD COLUMN installations_contract_move_in_date date NULL,
	ADD COLUMN installations_contract_move_out_date date NULL,
	ADD COLUMN installations_tdd_class varchar NULL,
	ADD COLUMN installations_address_street varchar NULL,
	ADD COLUMN installations_address_house_number varchar NULL,
	ADD COLUMN installations_address_house_org_number varchar NULL,
	ADD COLUMN installations_address_city varchar NULL,
	ADD COLUMN installations_address_city_part varchar NULL,
	ADD COLUMN installations_address_post_code varchar NULL,
	ADD COLUMN installations_address_country varchar NULL,
	ADD COLUMN installations_address_ruian_id int8 NULL;

-- Only one installation fits the invoice header, so the lowest place_id per invoice is restored.
UPDATE enapo_ppas_distribution_invoice i
SET
	eic = s.eic,
	place_id = s.place_id,
	installations_billing_class = s.billing_class,
	installations_measurement_type = s.measurement_type,
	installations_contract_contract_id = s.contract_contract_id,
	installations_contract_move_in_date = s.contract_move_in_date,
	installations_contract_move_out_date = s.contract_move_out_date,
	installations_tdd_class = s.tdd_class,
	installations_address_street = s.address_street,
	installations_address_house_number = s.address_house_number,
	installations_address_house_org_number = s.address_house_org_number,
	installations_address_city = s.address_city,
	installations_address_city_part = s.address_city_part,
	installations_address_post_code = s.address_post_code,
	installations_address_country = s.address_country,
	installations_address_ruian_id = s.address_ruian_id
FROM (
	SELECT DISTINCT ON (invoice_id, customer_id) *
	FROM enapo_ppas_distribution_installation
	ORDER BY invoice_id, customer_id, place_id
) s
WHERE i.id = s.invoice_id AND i.customer_id = s.customer_id;

CREATE INDEX idx_ppas_invoice_eic ON enapo_ppas_distribution_invoice(eic);
CREATE INDEX idx_ppas_invoice_place_id ON enapo_ppas_distribution_invoice(place_id);

DROP TABLE enapo_ppas_distribution_installation;
