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) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    email VARCHAR(100) NOT NULL
);

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

CREATE TABLE employeeStore (
    employeeId INT NOT NULL,
    storeId INT NOT NULL,
    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) NOT NULL,
    phone VARCHAR(20) NOT NULL
);

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

CREATE TABLE adoption (
    adoptionId INT AUTO_INCREMENT PRIMARY KEY,
    petId INT NOT NULL,
    customerId INT NOT NULL,
    adoptionDate DATE NOT NULL,
    adoptionStatus VARCHAR(20) NOT NULL,
    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) NOT NULL,
    supContactLastName VARCHAR(50) NOT NULL,
    supEmail VARCHAR(100) NOT NULL,
    supPhone VARCHAR(20) NOT NULL
);

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

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

CREATE TABLE productSupplier (
    supId INT NOT NULL,
    prodId INT NOT NULL,
    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 NOT NULL,
    quantity INT DEFAULT 0 NOT NULL,
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

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

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

CREATE TABLE appointmentPet (
    appointmentId INT NOT NULL,
    petId INT NOT NULL,
    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 NOT NULL,
    totalAmount DECIMAL(10, 2) NOT NULL,
    paymentMethod VARCHAR(50) NOT NULL,
    employeeId INT NOT NULL,
    storeId INT NOT NULL,
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
    FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);

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

CREATE TABLE activityLog (
    logId INT AUTO_INCREMENT PRIMARY KEY,
    employeeId INT NOT NULL,
    activity TEXT NOT NULL,
    logTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    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'),
('West Side Store', '789 West Blvd', '555-123-4567', 'westside@petshop.com'),
('East End Shop', '321 East Road', '555-987-6543', 'eastend@petshop.com'),
('South Mall Location', '654 South Plaza', '555-246-8135', 'southmall@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),
('Michael', 'Johnson', 'michael@petshop.com', '222-333-4444', 'Groomer', TRUE),
('Lisa', 'Williams', 'lisa@petshop.com', '333-444-5555', 'Staff', TRUE),
('David', 'Brown', 'david@petshop.com', '555-666-7777', 'Veterinarian', TRUE),
('Emma', 'Davis', 'emma@petshop.com', '666-777-8888', 'Manager', FALSE);

INSERT INTO employeeStore (employeeId, storeId)
VALUES
(1, 1),
(2, 1),
(2, 2),
(3, 2),
(4, 3),
(5, 1),
(5, 4),
(6, 5);

INSERT INTO customer (firstName, lastName, email, phone)
VALUES
('Alex', 'Brown', 'alex@gmail.com', '777-888-9999'),
('Emily', 'Clark', 'emily@gmail.com', '666-555-4444'),
('James', 'Wilson', 'james@gmail.com', '888-999-0000'),
('Olivia', 'Martinez', 'olivia@gmail.com', '999-000-1111'),
('William', 'Anderson', 'william@gmail.com', '000-111-2222'),
('Sophia', 'Taylor', 'sophia@gmail.com', '111-222-3333');

INSERT INTO pet (petName, petSpecies, petBreed, petAge, petStatus, petPrice)
VALUES
('Buddy', 'Dog', 'Labrador', 2, 'Available', 500.00),
('Milo', 'Cat', 'Persian', 1, 'Available', 300.00),
('Charlie', 'Dog', 'Golden Retriever', 3, 'Available', 550.00),
('Luna', 'Cat', 'Siamese', 2, 'Adopted', 350.00),
('Max', 'Dog', 'Beagle', 1, 'Available', 450.00),
('Bella', 'Cat', 'Maine Coon', 4, 'Available', 400.00);

INSERT INTO adoption (petId, customerId, adoptionDate, adoptionStatus)
VALUES
(1, 1, '2026-01-15', 'Completed'),
(4, 3, '2026-01-20', 'Completed'),
(2, 2, '2026-01-25', 'Pending'),
(5, 4, '2026-02-01', 'Completed'),
(6, 5, '2026-02-02', 'Pending');

INSERT INTO supplier (supCompany, supContactFirstName, supContactLastName, supEmail, supPhone)
VALUES
('PetFood Inc', 'Robert', 'King', 'contact@petfood.com', '888-111-2222'),
('Toy World', 'Jennifer', 'Lee', 'sales@toyworld.com', '888-222-3333'),
('Pet Supplies Co', 'Kevin', 'White', 'info@petsupplies.com', '888-333-4444'),
('Animal Care Products', 'Nancy', 'Green', 'orders@animalcare.com', '888-444-5555'),
('Premium Pet Goods', 'Tom', 'Black', 'support@premiumpet.com', '888-555-6666');

INSERT INTO category (categoryName, categoryType)
VALUES
('Dog Food', 'Product'),
('Cat Toys', 'Product'),
('Bird Supplies', 'Product'),
('Aquarium', 'Product'),
('Small Animals', 'Product');

INSERT INTO product (prodName, prodPrice, categoryId, prodDesc)
VALUES
('Premium Dog Food', 50.00, 1, 'High quality dog food'),
('Cat Toy Ball', 10.00, 2, 'Colorful toy for cats'),
('Bird Cage Large', 120.00, 3, 'Spacious bird cage'),
('Fish Tank 20 Gallon',  80.00, 4, 'Complete aquarium kit'),
('Hamster Wheel',  15.00, 5, 'Exercise wheel for small pets'),
('Organic Dog Treats', 25.00, 1, 'Natural dog treats');

INSERT INTO productSupplier (supId, prodId)
VALUES
(1, 1),
(1, 2),
(2, 2),
(3, 3),
(3, 4),
(4, 5),
(5, 6),
(1, 6);

INSERT INTO inventory (prodId, quantity)
VALUES
(1, 100),
(2, 200),
(3, 50),
(4, 30),
(5, 150),
(6, 75);

INSERT INTO service (serviceName, serviceDesc, serviceDuration, servicePrice)
VALUES
('Pet Grooming', 'Full grooming service', 60, 40.00),
('Nail Trimming', 'Quick nail trim', 15, 10.00),
('Bath and Brush', 'Bathing and brushing service', 45, 30.00),
('Veterinary Checkup', 'Complete health examination', 30, 75.00),
('Teeth Cleaning', 'Professional dental cleaning', 90, 100.00);

INSERT INTO appointment (serviceId, customerId, appointmentDate, appointmentTime, appointmentStatus)
VALUES
(1, 2, '2026-02-01', '10:30:00', 'Booked'),
(2, 1, '2026-02-03', '14:00:00', 'Booked'),
(3, 3, '2026-02-05', '09:00:00', 'Completed'),
(4, 4, '2026-02-07', '11:30:00', 'Booked'),
(5, 5, '2026-02-10', '15:00:00', 'Cancelled');

INSERT INTO appointmentPet (appointmentId, petId)
VALUES
(1, 2),
(2, 1),
(3, 3),
(4, 5),
(5, 6);

INSERT INTO sale (saleDate, totalAmount, paymentMethod, employeeId, storeId)
VALUES
(NOW(), 60.00, 'Card', 2, 1),
('2026-01-28 10:30:00', 50.00, 'Cash', 1, 1),
('2026-01-29 14:15:00', 120.00, 'Card', 4, 3),
('2026-01-30 16:45:00', 80.00, 'Card', 2, 2),
('2026-02-01 11:20:00', 150.00, 'Cash', 5, 4);

INSERT INTO saleItem (saleId, prodId, quantity, unitPrice)
VALUES
(1, 2, 2, 10.00),
(2, 1, 1, 50.00),
(3, 3, 1, 120.00),
(4, 4, 1, 80.00),
(5, 6, 6, 25.00),
(2, 2, 3, 10.00);

INSERT INTO activityLog (employeeId, activity)
VALUES
(1, 'Created new sale'),
(2, 'Booked appointment'),
(3, 'Completed grooming service'),
(4, 'Processed inventory order'),
(5, 'Conducted health checkup'),
(1, 'Updated customer information');