CREATE DATABASE IF NOT EXISTS flower_shop DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE flower_shop;

CREATE TABLE IF NOT EXISTS users (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(32) NOT NULL UNIQUE,
  password_hash VARCHAR(64) NOT NULL,
  role VARCHAR(16) NOT NULL,
  nickname VARCHAR(32),
  phone VARCHAR(32),
  email VARCHAR(64),
  avatar_url VARCHAR(255),
  disabled TINYINT(1) DEFAULT 0,
  created_at DATETIME
);

CREATE TABLE IF NOT EXISTS sessions (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  token VARCHAR(64) NOT NULL UNIQUE,
  expired TINYINT(1) DEFAULT 0,
  created_at DATETIME
);

CREATE TABLE IF NOT EXISTS addresses (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT NOT NULL,
  recipient_name VARCHAR(32) NOT NULL,
  phone VARCHAR(32) NOT NULL,
  province VARCHAR(32),
  city VARCHAR(32),
  district VARCHAR(32),
  detail VARCHAR(255),
  is_default TINYINT(1) DEFAULT 0
);

CREATE TABLE IF NOT EXISTS categories (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(32) NOT NULL,
  sort_order INT DEFAULT 0
);

CREATE TABLE IF NOT EXISTS products (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  category_id BIGINT,
  name VARCHAR(64) NOT NULL,
  description TEXT,
  price DECIMAL(10,2) NOT NULL,
  stock INT DEFAULT 0,
  cover_url VARCHAR(255),
  status VARCHAR(16) DEFAULT 'ON',
  created_at DATETIME,
  updated_at DATETIME
);

CREATE TABLE IF NOT EXISTS orders (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_no VARCHAR(32) NOT NULL UNIQUE,
  user_id BIGINT NOT NULL,
  status VARCHAR(16) NOT NULL,
  total_amount DECIMAL(10,2) NOT NULL,
  receiver_name VARCHAR(32),
  receiver_phone VARCHAR(32),
  receiver_address VARCHAR(128),
  created_at DATETIME,
  updated_at DATETIME,
  pay_at DATETIME
);

CREATE TABLE IF NOT EXISTS order_items (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT NOT NULL,
  product_id BIGINT NOT NULL,
  product_name VARCHAR(64) NOT NULL,
  product_price DECIMAL(10,2) NOT NULL,
  quantity INT NOT NULL,
  product_cover VARCHAR(255)
);

CREATE TABLE IF NOT EXISTS reviews (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT,
  product_id BIGINT,
  user_id BIGINT,
  rating INT,
  content VARCHAR(1000),
  images VARCHAR(1000),
  status VARCHAR(16) DEFAULT 'PENDING',
  created_at DATETIME
);

CREATE TABLE IF NOT EXISTS confessions (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  order_id BIGINT,
  user_id BIGINT,
  code VARCHAR(32) NOT NULL UNIQUE,
  title VARCHAR(64),
  message VARCHAR(2000),
  image_url VARCHAR(255),
  created_at DATETIME
);

CREATE TABLE IF NOT EXISTS barrages (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  confession_id BIGINT NOT NULL,
  sender VARCHAR(32),
  content VARCHAR(200) NOT NULL,
  created_at DATETIME
);

INSERT INTO users (username, password_hash, role, nickname, created_at)
VALUES ('admin', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92', 'ADMIN', '管理员', NOW())
ON DUPLICATE KEY UPDATE username = username;

INSERT INTO categories (name, sort_order) VALUES
('表白花束', 1),
('生日花束', 2),
('节日限定', 3)
ON DUPLICATE KEY UPDATE name = VALUES(name);

INSERT INTO products (category_id, name, description, price, stock, cover_url, status, created_at, updated_at) VALUES
(1, '心动红玫瑰', '11支红玫瑰搭配满天星', 199.00, 50, 'https://via.placeholder.com/400x300?text=Rose', 'ON', NOW(), NOW()),
(2, '暖阳向日葵', '向日葵搭配桔梗', 168.00, 40, 'https://via.placeholder.com/400x300?text=Sunflower', 'ON', NOW(), NOW()),
(3, '节日限定花篮', '节日限定主题花篮', 299.00, 20, 'https://via.placeholder.com/400x300?text=Holiday', 'ON', NOW(), NOW());
