-- Sample "shop" database for the Learn SQL series on victorhugosolis.com
-- Works in PostgreSQL, MySQL, SQLite, and SQL Server.

-- Views and tables created later in the series depend on these, so drop them first.
-- (In PostgreSQL, also run DROP MATERIALIZED VIEW IF EXISTS monthly_sales; if you created it.)
DROP VIEW IF EXISTS customer_summary;
DROP VIEW IF EXISTS order_details;
DROP VIEW IF EXISTS ca_customers;
DROP TABLE IF EXISTS reviews;
DROP TABLE IF EXISTS orders_archive;
DROP TABLE IF EXISTS refunds;
DROP TABLE IF EXISTS price_history;
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
DROP TABLE IF EXISTS customers;

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    first_name  VARCHAR(50) NOT NULL,
    last_name   VARCHAR(50) NOT NULL,
    email       VARCHAR(100),
    city        VARCHAR(50),
    state       CHAR(2),
    signup_date DATE NOT NULL
);

CREATE TABLE products (
    product_id INT PRIMARY KEY,
    name       VARCHAR(50) NOT NULL,
    category   VARCHAR(30) NOT NULL,
    price      DECIMAL(8,2) NOT NULL
);

CREATE TABLE orders (
    order_id    INT PRIMARY KEY,
    customer_id INT NOT NULL,
    product_id  INT NOT NULL,
    quantity    INT NOT NULL,
    order_date  DATE NOT NULL,
    status      VARCHAR(20) NOT NULL,
    FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
    FOREIGN KEY (product_id)  REFERENCES products (product_id)
);

INSERT INTO customers VALUES
    (1, 'Maria',  'Lopez',    'maria.lopez@example.com',  'El Centro', 'CA', '2025-01-15'),
    (2, 'James',  'Carter',   'james.carter@example.com', 'San Diego', 'CA', '2025-02-03'),
    (3, 'Sofia',  'Nguyen',   'sofia.nguyen@example.com', 'Phoenix',   'AZ', '2025-02-20'),
    (4, 'Daniel', 'Brooks',   NULL,                       'Yuma',      'AZ', '2025-03-08'),
    (5, 'Ana',    'Martinez', 'ana.martinez@example.com', 'El Centro', 'CA', '2025-04-12'),
    (6, 'Kevin',  'Patel',    'kevin.patel@example.com',  'Las Vegas', 'NV', '2025-05-30'),
    (7, 'Laura',  'Chen',     'laura.chen@example.com',   NULL,        'NV', '2025-06-18'),
    (8, 'Omar',   'Haddad',   'omar.haddad@example.com',  'San Diego', 'CA', '2025-07-04');

INSERT INTO products VALUES
    (1, 'Laptop',       'Electronics', 899.00),
    (2, 'Monitor',      'Electronics', 249.00),
    (3, 'Keyboard',     'Accessories',  45.00),
    (4, 'Mouse',        'Accessories',  25.00),
    (5, 'Desk',         'Furniture',   320.00),
    (6, 'Office Chair', 'Furniture',   180.00);

INSERT INTO orders VALUES
    (101, 1, 1, 1, '2025-08-02', 'shipped'),
    (102, 2, 4, 3, '2025-08-05', 'shipped'),
    (103, 1, 3, 2, '2025-08-11', 'shipped'),
    (104, 3, 5, 1, '2025-08-19', 'shipped'),
    (105, 5, 2, 2, '2025-09-01', 'shipped'),
    (106, 2, 1, 1, '2025-09-07', 'returned'),
    (107, 6, 6, 4, '2025-09-15', 'shipped'),
    (108, 4, 4, 5, '2025-09-22', 'pending'),
    (109, 8, 1, 2, '2025-10-03', 'shipped'),
    (110, 3, 2, 1, '2025-10-10', 'shipped'),
    (111, 5, 6, 2, '2025-10-21', 'cancelled'),
    (112, 1, 5, 1, '2025-11-02', 'pending');
