-- ┌────────────────────┐
-- │STORE TABLE AND DATA│
-- └────────────────────┘
DROP TABLE IF EXISTS STORE;

CREATE TABLE IF NOT EXISTS STORE (
  Store_key INTEGER PRIMARY KEY,
  City TEXT NOT NULL,
  Region TEXT CHECK (Region IN ('East', 'Central', 'West')) NOT NULL
);

INSERT INTO
  STORE (Store_key, City, Region)
VALUES
  (1, 'New York', 'East'),
  (2, 'Chicago', 'Central'),
  (3, 'Atlanta', 'East'),
  (4, 'Los Angeles', 'West'),
  (5, 'San Francisco', 'West'),
  (6, 'Philadelphia', 'East');

-- ┌──────────────────────┐
-- │PRODUCT TABLE AND DATA│
-- └──────────────────────┘
DROP TABLE IF EXISTS PRODUCT;

CREATE TABLE IF NOT EXISTS PRODUCT (
  Product_key INTEGER PRIMARY KEY,
  Description TEXT NOT NULL,
  Brand TEXT CHECK (
    Brand IN (
      'MKF Studios',
      'Wolf',
      'Parabuster Inc.',
      'Big Studios'
    )
  ) NOT NULL
);

INSERT INTO
  PRODUCT (Product_key, Description, Brand)
VALUES
  (1, 'Beautiful Girls', 'MKF Studios'),
  (2, 'Toy Story', 'Wolf'),
  (3, 'Sense and Sensibility', 'Parabuster Inc.'),
  (4, 'Holday of the Year', 'Wolf'),
  (5, 'Pulp Fiction', 'MKF Studios'),
  (6, 'The Juror', 'MKF Studios'),
  (7, 'From Dusk Till Dawn', 'Parabuster Inc.'),
  (8, 'Hellraiser: Bloodline', 'Big Studios');

-- ┌─────────────────────────┐
-- │SALES_FACT TABLE AND DATA│
-- └─────────────────────────┘
DROP TABLE IF EXISTS SALES_FACT;

-- WARN: Storing money AS floats/`REAL` is a big no-no in a real application due to floating point
-- rounding errors. For this purpose it's ok, but for the love of god and all that is holy do NOT
-- store money AS a float.
CREATE TABLE IF NOT EXISTS SALES_FACT (
  Store_key INTEGER NOT NULL,
  Product_key INTEGER NOT NULL,
  Sales REAL NOT NULL,
  Cost REAL NOT NULL,
  Profit REAL NOT NULL,
  FOREIGN KEY (Store_key) REFERENCES STORE (Store_key),
  FOREIGN KEY (Product_key) REFERENCES PRODUCT (Product_key)
);

INSERT INTO
  SALES_FACT (Store_key, Product_key, Sales, Cost, Profit)
VALUES
  (1, 6, 2.39, 1.15, 1.24),
  (1, 2, 16.7, 6.91, 9.79),
  (2, 7, 7.16, 2.75, 4.40),
  (3, 2, 4.77, 1.84, 2.93),
  (5, 3, 11.93, 4.59, 7.34),
  (5, 1, 14.31, 5.51, 8.80);

--- Answers to Questions ---
-- 1. What was the total profit across all stores?
SELECT
  Sum(Profit) AS "Total Profit Across All Stores"
FROM
  SALES_FACT;

-- 2. What is the name of the most popular product sold?
SELECT
  Description AS "Name of Most Popular Product"
FROM
  (
    SELECT
      Max(sf.Sales),
      p.Description
    FROM
      SALES_FACT AS sf
      INNER JOIN PRODUCT p ON p.Product_key = sf.Product_key
  );

-- 3. How much profit did the "East" region make?
SELECT
  Sum(Profit) AS "East Region Profits"
FROM
  SALES_FACT AS sf
  INNER JOIN STORE AS s ON s.Store_key = sf.Store_key
  AND s.REGION = 'East';

-- 4. Which city had the highest cost?
SELECT
  City AS "City With Highest Aggregate Cost"
FROM
  (
    SELECT
      s.Store_key,
      Max(cost_sums) AS "Total Cost",
      City
    FROM
      (
        SELECT
          Store_key,
          Sum(Cost) AS cost_sums
        FROM
          SALES_FACT
        GROUP BY
          Store_key
      ) AS sf
      INNER JOIN STORE AS s ON s.Store_key = sf.Store_key
  );

SELECT
  City AS "City With Single Highest Cost"
FROM
  (
    SELECT
      s.Store_key,
      Max(cost_sums) AS "Total Cost",
      City
    FROM
      SALES_FACT AS sf
    GROUP BY
      Store_key
      INNER JOIN STORE AS s ON s.Store_key = sf.Store_key
  );

-- 5. What was the least popular product brand?
SELECT
  Brand as "Least Popular Brand"
FROM
  (
    SELECT
      Brand,
      Min(sales_sums) as t
    FROM
      (
        SELECT
          Product_key,
          Sum(Sales) AS sales_sums
        FROM
          SALES_FACT
        GROUP BY
          Product_key
      ) AS sf
      LEFT JOIN PRODUCT AS p ON sf.Product_key = p.Product_key
  );

-- 6. Which city (or cities) sold "Sense and Sensibility"?
SELECT
  City AS "City That Sold 'Sense and Sensibility'"
FROM
  STORE AS s
WHERE
  s.Store_key = (
    SELECT
      sf.Store_key
    FROM
      SALES_FACT as sf
    WHERE
      sf.Product_key = (
        SELECT
          p.Product_key
        FROM
          PRODUCT AS p
        WHERE
          p.Description = 'Sense and Sensibility'
      )
  );
