-- Create Tables

CREATE TABLE IF NOT EXISTS storeLocation (
    storeId BIGINT AUTO_INCREMENT PRIMARY KEY,
    storeName VARCHAR(100) NOT NULL,
    address VARCHAR(255) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS employee (
    employeeId BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NULL,
    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,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT uk_employee_user_id UNIQUE (user_id)
);

CREATE TABLE IF NOT EXISTS employeeStore (
    employeeId BIGINT NOT NULL,
    storeId BIGINT NOT NULL,
    PRIMARY KEY (employeeId, storeId),
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
    FOREIGN KEY (storeId) REFERENCES storeLocation(storeId)
);

CREATE TABLE IF NOT EXISTS customer (
    customerId BIGINT AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT NULL,
    firstName VARCHAR(50) NOT NULL,
    lastName VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    CONSTRAINT uk_customer_user_id UNIQUE (user_id)
);

CREATE TABLE IF NOT EXISTS pet (
    petId BIGINT 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,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS adoption (
    adoptionId BIGINT AUTO_INCREMENT PRIMARY KEY,
    petId BIGINT NOT NULL,
    customerId BIGINT NOT NULL,
    adoptionDate DATE NOT NULL,
    adoptionStatus VARCHAR(20) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (petId) REFERENCES pet(petId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId)
);

CREATE TABLE IF NOT EXISTS supplier (
    supId BIGINT 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,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS category (
    categoryId BIGINT AUTO_INCREMENT PRIMARY KEY,
    categoryName VARCHAR(100) NOT NULL,
    categoryType VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS product (
    prodId BIGINT AUTO_INCREMENT PRIMARY KEY,
    prodName VARCHAR(100) NOT NULL,
    prodPrice DECIMAL(10, 2) NOT NULL,
    categoryId BIGINT NOT NULL,
    prodDesc TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (categoryId) REFERENCES category(categoryId)
);

CREATE TABLE IF NOT EXISTS productSupplier (
    supId BIGINT NOT NULL,
    prodId BIGINT NOT NULL,
    cost DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (supId, prodId),
    FOREIGN KEY (supId) REFERENCES supplier(supId),
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE IF NOT EXISTS inventory (
    inventoryId BIGINT AUTO_INCREMENT PRIMARY KEY,
    prodId BIGINT NOT NULL,
    quantity INT DEFAULT 0 NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE IF NOT EXISTS service (
    serviceId BIGINT AUTO_INCREMENT PRIMARY KEY,
    serviceName VARCHAR(100) NOT NULL,
    serviceDesc TEXT,
    serviceDuration INT NOT NULL,
    servicePrice DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS appointment (
    appointmentId BIGINT AUTO_INCREMENT PRIMARY KEY,
    serviceId BIGINT NOT NULL,
    customerId BIGINT NOT NULL,
    appointmentDate DATE NOT NULL,
    appointmentTime TIME NOT NULL,
    appointmentStatus VARCHAR(20) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (serviceId) REFERENCES service(serviceId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId)
);

CREATE TABLE IF NOT EXISTS appointmentPet (
    appointmentId BIGINT NOT NULL,
    petId BIGINT NOT NULL,
    PRIMARY KEY (appointmentId, petId),
    FOREIGN KEY (appointmentId) REFERENCES appointment(appointmentId),
    FOREIGN KEY (petId) REFERENCES pet(petId)
);

CREATE TABLE IF NOT EXISTS sale (
    saleId BIGINT AUTO_INCREMENT PRIMARY KEY,
    saleDate DATETIME NOT NULL,
    totalAmount DECIMAL(10, 2) NOT NULL,
    paymentMethod VARCHAR(50) NOT NULL,
    employeeId BIGINT NOT NULL,
    storeId BIGINT NOT NULL,
    customerId BIGINT NULL,
    isRefund BOOLEAN DEFAULT FALSE NOT NULL,
    originalSaleId BIGINT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId),
    FOREIGN KEY (storeId) REFERENCES storeLocation(storeId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId),
    FOREIGN KEY (originalSaleId) REFERENCES sale(saleId)
);

CREATE TABLE IF NOT EXISTS saleItem (
    saleItemId BIGINT AUTO_INCREMENT PRIMARY KEY,
    saleId BIGINT NOT NULL,
    prodId BIGINT NOT NULL,
    quantity INT NOT NULL,
    unitPrice DECIMAL(10, 2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (saleId) REFERENCES sale(saleId),
    FOREIGN KEY (prodId) REFERENCES product(prodId)
);

CREATE TABLE IF NOT EXISTS purchaseOrder (
    purchaseOrderId BIGINT AUTO_INCREMENT PRIMARY KEY,
    supId BIGINT NOT NULL,
    orderDate DATE NOT NULL,
    status VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (supId) REFERENCES supplier(supId)
);

CREATE TABLE IF NOT EXISTS activityLog (
    logId BIGINT AUTO_INCREMENT PRIMARY KEY,
    employeeId BIGINT NOT NULL,
    activity TEXT NOT NULL,
    logTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    FOREIGN KEY (employeeId) REFERENCES employee(employeeId)
);

CREATE TABLE IF NOT EXISTS users (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100) UNIQUE,
    fullName VARCHAR(100),
    avatarUrl VARCHAR(255),
    role VARCHAR(20) NOT NULL,
    active BOOLEAN NOT NULL DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS refund (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    saleId BIGINT NOT NULL,
    customerId BIGINT NOT NULL,
    amount DECIMAL(10, 2) NOT NULL,
    reason VARCHAR(500) NOT NULL,
    status VARCHAR(20) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (saleId) REFERENCES sale(saleId),
    FOREIGN KEY (customerId) REFERENCES customer(customerId)
);

CREATE TABLE IF NOT EXISTS conversation (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    customerId BIGINT NOT NULL,
    staffId BIGINT,
    status VARCHAR(20) DEFAULT 'OPEN',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (customerId) REFERENCES customer(customerId),
    FOREIGN KEY (staffId) REFERENCES users(id)
);

CREATE TABLE IF NOT EXISTS message (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    conversationId BIGINT NOT NULL,
    senderId BIGINT NOT NULL,
    content TEXT NOT NULL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    isRead BOOLEAN DEFAULT FALSE,
    FOREIGN KEY (conversationId) REFERENCES conversation(id),
    FOREIGN KEY (senderId) REFERENCES users(id)
);

-- Add foreign keys for user_id linkage
ALTER TABLE employee ADD CONSTRAINT fk_employee_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
ALTER TABLE customer ADD CONSTRAINT fk_customer_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;
