-- Question 1 part 1 a)
CREATE TABLE Donors
(
    donorID       INT         NOT NULL,
    firstName     VARCHAR(50) NOT NULL,
    lastName      VARCHAR(50) NOT NULL,
    middleInitial CHAR(1),
    dateOfBirth   DATE,
    address       VARCHAR(100), -- might need to change to civic code only
    city          VARCHAR(50),
    postalCode    VARCHAR(7),
    province      ENUM (
                      'AB',                   -- Alberta
                      'BC',                   -- British Columbia
                      'MB',                   -- Manitoba
                      'NB',                   -- New Brunswick
                      'NL',                   -- Newfoundland and Labrador
                      'NS',                   -- Nova Scotia
                      'NT',                   -- Northwest Territories
                      'NU',                   -- Nunavut
                      'ON',                   -- Ontario
                      'PE',                   -- Prince Edward Island
                      'QC',                   -- Quebec
                      'SK',                   -- Saskatchewan
                      'YT'                    -- Yukon
                      ),
    gender        ENUM ('M', 'F', 'U'),
    SSN           VARCHAR(11),
    hobby         VARCHAR(100),
    phone         VARCHAR(15),
    email         VARCHAR(100),
    CONSTRAINT donors_pk PRIMARY KEY (donorID)
);

CREATE TABLE Donations
(
    dID     INT            NOT NULL,
    donorID INT            NOT NULL,
    date    DATE           NOT NULL,
    type    VARCHAR(7)     NOT NULL,
    amount  DECIMAL(10, 2) NOT NULL,
    CONSTRAINT donations_pk PRIMARY KEY (dID),
    CONSTRAINT donations_donorID_fk FOREIGN KEY (donorID) REFERENCES Donors (donorID),
    CONSTRAINT valid_donation_type CHECK (type IN ('money', 'product'))
);

create table Products
(
    pID         INT            NOT NULL,
    description varchar(100),
    date        DATE           NOT NULL,
    price       DECIMAL(10, 2) NOT NULL,
    weight      DECIMAL(5, 2)  NOT NULL,
    CONSTRAINT products_pk PRIMARY KEY (pID)
);

CREATE TABLE Sales
(
    sID         INT                      NOT NULL,
    date        DATE                     NOT NULL,
    amount      DECIMAL(10, 2)           NOT NULL,
    totalWeight DECIMAL(10, 2)           NOT NULL,
    deliveryFee DECIMAL(10, 2) DEFAULT 0 NOT NULL,
    CONSTRAINT sales_pk primary key (sID),
    CONSTRAINT valid_delivery_fee CHECK (deliveryFee >= 0)
);

CREATE TABLE SalesItems
(
    sID INT NOT NULL,
    pID INT NOT NULL,
    PRIMARY KEY (sID, pID),
    FOREIGN KEY (sID) REFERENCES Sales (sID),
    FOREIGN KEY (pID) REFERENCES Products (pID)
);

-- Question 1 part 1 b)
ALTER TABLE Donors
    DROP COLUMN hobby;

-- Question 1 part 1 c)
ALTER TABLE Products
    ADD location VARCHAR(50) DEFAULT 'Unknown';
ALTER TABLE Products
    ADD color VARCHAR(10) DEFAULT 'Unknown';

-- Question 1 part 1 d)
-- !!! ASSUME THAT HOBBY COLUMN IS NO LONGER THERE
INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province,
                    gender, SSN, phone, email)
VALUES (1, 'John', 'Smith', 'A', '1985-03-15', '123 Maple St', 'Toronto', 'M5V 2T6', 'ON', 'M', '123-456-789',
        '(416) 555-1234', 'john.smith@email.com');

INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province,
                    gender, SSN, phone, email)
VALUES (2, 'Emily', 'Johnson', 'M', '1990-07-22', '456 Oak Ave', 'Vancouver', 'V6B 3K9', 'BC', 'F', '987-654-321',
        '(604) 555-5678', 'emily.j@email.com');

INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province,
                    gender, SSN, phone, email)
VALUES (3, 'Alex', 'Lee', NULL, '1988-11-30', '789 Pine Rd', 'Montreal', 'H3A 1A1', 'QC', 'U', '456-789-123',
        '(514) 555-9012', 'alex.lee@email.com');

INSERT INTO Donations (dID, donorID, date, type, amount)
VALUES (1, 1, '2023-05-15', 'money', 100.00);

INSERT INTO Donations (dID, donorID, date, type, amount)
VALUES (2, 2, '2023-06-22', 'product', 75.50);

INSERT INTO Donations (dID, donorID, date, type, amount)
VALUES (3, 3, '2023-07-03', 'money', 250.00);

-- Question 1 part 1 e)
DELETE
FROM Donors
WHERE donorID > 0;
DELETE
FROM Donations
WHERE donorID > 0;

-- Question 1 part 1 f)
-- We have not covered ON DELETE/ ON UPDATE CASCADE
-- in class so wer have to delete in such order of statements
DROP TABLE Donations;
DROP TABLE Donors;
DROP TABLE SalesItems;
DROP TABLE Products;
DROP TABLE Sales;

-- Question 1 part 2 a)
SELECT Donors.donorID,
       Donors.firstName,
       Donors.lastName,
       Donors.middleInitial,
       Donors.dateOfBirth,
       Donors.phone,
       Donors.email,
       Donors.SSN
FROM Donors
WHERE (Donors.gender = 'M')
  AND (Donors.city = 'Montreal' OR Donors.city = 'Laval');

-- Question 1 part 2 b)
SELECT Sales.sID, Products.pID, Products.description, Products.price, Products.weight
FROM Sales,
     Products
WHERE Products.color = 'red'
  AND Sales.date = '2024-02-14';

-- Question 1 part 2 c)
-- One sID can have multiple products
-- We need the DISTINCT keyword
SELECT COUNT(DISTINCT sID)
FROM Sales
WHERE Sales.date = '2024-02-14';

-- Question 1 part 2 d)
SELECT EXTRACT(MONTH FROM date) AS month,
       COUNT(DISTINCT sID)      AS total_sales,
       SUM(amount)              AS total_amount,
       SUM(deliveryFee)         AS total_delivery_fees
FROM Sales
WHERE EXTRACT(YEAR FROM date) = 2023
GROUP BY EXTRACT(MONTH FROM date)
ORDER BY month;

-- Question 1 part 2 e)
SELECT Donors.gender, Donors.lastName, Donors.firstName, SUM(Donations.amount) AS total_donation_amount
FROM Donors,
     Donations
WHERE Donors.city = 'Brossard'
  AND (Donations.date > '2022-01-01')
  AND (Donations.date < '2023-12-31')
GROUP BY Donors.donorID, Donors.gender, Donors.lastName, Donors.firstName
ORDER BY Donors.gender, Donors.lastName, Donors.firstName;

