DROP TABLE IF EXISTS addressRelations;
CREATE TABLE addressRelations (addr1 int, addr2 int);
CREATE TEMPORARY TABLE tempRelations (addr1 int, addr2 int);
-------------------------------------
-- Tx that has more than one input
CREATE OR REPLACE VIEW joint_control_transactions
AS SELECT tx_id
FROM inputs
GROUP BY tx_id
HAVING count(*)>1;
-------------------------------------
-- Tx that pass serial control check
CREATE OR REPLACE VIEW serial_control_transactions
AS SELECT tx_id
FROM inputs
JOIN outputs
USING (tx_id)
GROUP BY tx_id
HAVING count(*)=1;
-------------------------------------
--insert joint control results (contains symmetric and reflexive results)
INSERT INTO tempRelations
SELECT inputs.sig_id, joint_tx_with_addr.sig_id 
FROM inputs 
RIGHT OUTER JOIN (
    SELECT tx_id, sig_id 
    FROM joint_control_transactions  
    JOIN inputs 
    USING (tx_id)
) 
AS joint_tx_with_addr 
USING (tx_id);
-------------------------------------
--insert serial control results
INSERT INTO tempRelations
SELECT sig_id, pk_id
FROM inputs 
JOIN outputs 
USING (tx_id)
WHERE tx_id IN (SELECT tx_id FROM serial_control_transactions) AND sig_id != 0;
-------------------------------------
-- clean up reflexivity
DELETE
FROM tempRelations
WHERE addr1 = addr2;
-------------------------------------
-- Temporary table for storing symmetrical rows
CREATE TEMPORARY TABLE temp_symmetry (addr1 int, addr2 int);

-- find symmetrical rows
INSERT INTO temp_symmetry
SELECT tmp1.addr1, tmp1.addr2
FROM tempRelations tmp1
INNER JOIN tempRelations tmp2
ON tmp1.addr1 = tmp2.addr2 
AND tmp1.addr2 = tmp2.addr1
AND tmp1.addr1 > tmp1.addr2;

-- clean up symmetrical rows
DELETE 
FROM tempRelations
WHERE EXISTS (SELECT * FROM temp_symmetry WHERE tempRelations.addr1 = temp_symmetry.addr1 AND tempRelations.addr2 = temp_symmetry.addr2);

INSERT INTO addressRelations
SELECT addr1, addr2
FROM tempRelations;

DROP TABLE IF EXISTS temp_symmetry;
DROP TABLE IF EXISTS tempRelations;

-- exercise c (2)
CREATE TEMPORARY TABLE temp_clusters (id int, address int);

INSERT INTO temp_clusters
SELECT id, address 
FROM clusterAddresses();

DROP TABLE IF EXISTS max_value_by_entity;
DROP TABLE IF EXISTS min_addr_of_max_entity;
DROP TABLE IF EXISTS max_tx_to_max_entity;

CREATE TABLE max_value_by_entity (value bigint);
CREATE TABLE min_addr_of_max_entity(addr int);
CREATE TABLE max_tx_to_max_entity(tx_id int); 

CREATE TEMPORARY TABLE temp_sum_values (id int, sum_values int);

INSERT INTO temp_sum_values
SELECT id, SUM(value) as sum_values
FROM temp_clusters temp1 INNER JOIN (
    SELECT utxos.value, pk_id
    FROM utxos
    INNER JOIN outputs
    USING (output_id)
) temp2
ON temp1.address = temp2.pk_id
GROUP BY id;

INSERT INTO max_value_by_entity
SELECT MAX(sum_values)
FROM temp_sum_values;

INSERT INTO min_addr_of_max_entity
SELECT MIN(address)
FROM temp_clusters
INNER JOIN (
    SELECT id
    FROM (
        SELECT id, MAX(sum_values) as max_value
        FROM temp_sum_values
    ) as temp
    WHERE temp.max_value = max_value_by_entity.value
) as temp USING (id);

INSERT INTO max_tx_to_max_entity
SELECT tx_id 
FROM outputs LEFT JOIN (
    SELECT MAX(value) as value
    FROM outputs LEFT JOIN (
        SELECT address
        FROM temp_clusters
        INNER JOIN (
            SELECT id
            FROM (
                SELECT id, MAX(sum_values) as max_value
                FROM temp_sum_values
            ) as temp
            WHERE temp.max_value = max_value_by_entity.value
        ) as join1 USING (id)
    ) as temp1 ON outputs.pk_id = temp1.address
) USING (value)

