DROP DATABASE IF EXISTS Petstoredb;
CREATE DATABASE Petstoredb;
USE Petstoredb;

-- Create Tables

CREATE TABLE storeLocation (
    storeId INT AUTO_INCREMENT PRIMARY KEY,
    storeName VARCHAR(100) NOT NULL,
    address VARCHAR(255),
    phone VARCHAR(20),
    email VARCHAR(100)
);

CREATE TABLE employee (
    employeeId INT AUTO_INCREMENT PRIMARY KEY,
    firstName VARCHAR(50) NOT NULL,
    lastName VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    phone VARCHAR(20),
    role VARCHAR(50),
    isActive BOOLEAN DEFAULT TRUE
);

CREATE TABLE employeeStore (
    employeeId INT,
    storeId INT,
    PRIMARY KEY (employeeId, storeId),
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
    FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);

CREATE TABLE customer (
    customerId INT AUTO_INCREMENT PRIMARY KEY,
    firstName VARCHAR(50) NOT NULL,
    lastName VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    phone VARCHAR(20)
);

CREATE TABLE pet (
    petId INT AUTO_INCREMENT PRIMARY KEY,
    petName VARCHAR(50),
    petSpecies VARCHAR(50),
    petBreed VARCHAR(50),
    petAge INT,
    petStatus VARCHAR(20),
    petPrice DECIMAL(10, 2)
);

CREATE TABLE adoption (
    adoptionId INT AUTO_INCREMENT PRIMARY KEY,
    petId INT,
    customerId INT,
    adoptionDate DATE,
    adoptionStatus VARCHAR(20),
    FOREIGN KEY (petId) REFERENCES pet(petId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId)
);

CREATE TABLE supplier (
    supId INT AUTO_INCREMENT PRIMARY KEY,
    supCompany VARCHAR(100) NOT NULL,
    supContactFirstName VARCHAR(50),
    supContactLastName VARCHAR(50),
    supEmail VARCHAR(100),
    supPhone VARCHAR(20)
);

CREATE TABLE category (
    categoryId INT AUTO_INCREMENT PRIMARY KEY,
    categoryName VARCHAR(100) NOT NULL,
    categoryType VARCHAR(50)
);

CREATE TABLE product (
    prodId INT AUTO_INCREMENT PRIMARY KEY,
    prodName VARCHAR(100) NOT NULL,
    prodSku VARCHAR(50) UNIQUE,
    prodPrice DECIMAL(10, 2),
    categoryId INT,
    prodDesc TEXT,
    FOREIGN KEY (categoryId) REFERENCES category(categoryId)
);

CREATE TABLE productSupplier (
    supId INT,
    prodId INT,
    PRIMARY KEY (supId, prodId),
    FOREIGN KEY (supId) REFERENCES supplier(supId),
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE inventory (
    inventoryId INT AUTO_INCREMENT PRIMARY KEY,
    prodId INT,
    quantity INT DEFAULT 0,
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE service (
    serviceId INT AUTO_INCREMENT PRIMARY KEY,
    serviceName VARCHAR(100) NOT NULL,
    serviceDesc TEXT,
    serviceDuration INT,
    servicePrice DECIMAL(10, 2)
);

CREATE TABLE appointment (
    appointmentId INT AUTO_INCREMENT PRIMARY KEY,
    serviceId INT,
    customerId INT,
    appointmentDate DATE,
    appointmentTime TIME,
    appointmentStatus VARCHAR(20),
    FOREIGN KEY (serviceId) REFERENCES service(serviceId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId)
);

CREATE TABLE appointmentPet (
    appointmentId INT,
    petId INT,
    PRIMARY KEY (appointmentId, petId),
    FOREIGN KEY (appointmentId) REFERENCES appointment(appointmentId),
    FOREIGN KEY (petId) REFERENCES pet(petId)
);

CREATE TABLE sale (
    saleId INT AUTO_INCREMENT PRIMARY KEY,
    saleDate DATETIME,
    totalAmount DECIMAL(10, 2),
    paymentMethod VARCHAR(50),
    employeeId INT,
    storeId INT,
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
    FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);

CREATE TABLE saleItem (
    saleItemId INT AUTO_INCREMENT PRIMARY KEY,
    saleId INT,
    prodId INT,
    quantity INT,
    unitPrice DECIMAL(10, 2),
    FOREIGN KEY (saleId) REFERENCES sale(saleId),
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE activityLog (
    logId INT AUTO_INCREMENT PRIMARY KEY,
    employeeId INT,
    activity TEXT,
    logTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId)
);

-- Insert Sample Data

INSERT INTO storeLocation (storeName, address, phone, email)
VALUES
('Downtown Branch', '123 Main St', '123-456-7890', 'downtown@petshop.com'),
('North Branch', '456 North Ave', '987-654-3210', 'north@petshop.com');

INSERT INTO employee (firstName, lastName, email, phone, role, isActive)
VALUES
('John', 'Doe', 'john@petshop.com', '111-222-3333', 'Manager', TRUE),
('Sara', 'Smith', 'sara@petshop.com', '444-555-6666', 'Staff', TRUE);

INSERT INTO employeeStore (employeeId, storeId)
VALUES
(1, 1),
(2, 1),
(2, 2);

INSERT INTO customer (firstName, lastName, email, phone)
VALUES
('Alex', 'Brown', 'alex@gmail.com', '777-888-9999'),
('Emily', 'Clark', 'emily@gmail.com', '666-555-4444');

INSERT INTO pet (petName, petSpecies, petBreed, petAge, petStatus, petPrice)
VALUES
('Buddy', 'Dog', 'Labrador', 2, 'Available', 500.00),
('Milo', 'Cat', 'Persian', 1, 'Available', 300.00);

INSERT INTO adoption (petId, customerId, adoptionDate, adoptionStatus)
VALUES
(1, 1, '2026-01-15', 'Completed');

INSERT INTO supplier (supCompany, supContactFirstName, supContactLastName, supEmail, supPhone)
VALUES
('PetFood Inc', 'Robert', 'King', 'contact@petfood.com', '888-111-2222');

INSERT INTO category (categoryName, categoryType)
VALUES
('Dog Food', 'Product'),
('Cat Toys', 'Product');

INSERT INTO product (prodName, prodSku, prodPrice, categoryId, prodDesc)
VALUES
('Premium Dog Food', 'DF001', 50.00, 1, 'High quality dog food'),
('Cat Toy Ball', 'CT001', 10.00, 2, 'Colorful toy for cats');

INSERT INTO productSupplier (supId, prodId)
VALUES
(1, 1),
(1, 2);

INSERT INTO inventory (prodId, quantity)
VALUES
(1, 100),
(2, 200);

INSERT INTO service (serviceName, serviceDesc, serviceDuration, servicePrice)
VALUES
('Pet Grooming', 'Full grooming service', 60, 40.00);

INSERT INTO appointment (serviceId, customerId, appointmentDate, appointmentTime, appointmentStatus)
VALUES
(1, 2, '2026-02-01', '10:30:00', 'Booked');

INSERT INTO appointmentPet (appointmentId, petId)
VALUES
(1, 2);

INSERT INTO sale (saleDate, totalAmount, paymentMethod, employeeId, storeId)
VALUES
(NOW(), 60.00, 'Card', 2, 1);

INSERT INTO saleItem (saleId, prodId, quantity, unitPrice)
VALUES
(1, 2, 2, 10.00);

INSERT INTO activityLog (employeeId, activity)
VALUES
(1, 'Created new sale'),
(2, 'Booked appointment');
