-- 创建数据库（如果不存在）
CREATE DATABASE IF NOT EXISTS smalltown;
USE smalltown;

-- 用户表
CREATE TABLE IF NOT EXISTS users (
    id VARCHAR(36) PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    password_hash VARCHAR(64) NOT NULL,
    token VARCHAR(64),
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_token (token),
    INDEX idx_username (username)
);

-- 房屋表
CREATE TABLE IF NOT EXISTS houses (
    id VARCHAR(36) PRIMARY KEY,
    owner VARCHAR(50) NOT NULL,
    title VARCHAR(200) NOT NULL,
    description TEXT,
    price INT NOT NULL,
    district VARCHAR(50) NOT NULL,
    address VARCHAR(500) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    images JSON,
    status VARCHAR(20) DEFAULT 'pending',
    reject_reason VARCHAR(500),
    reviewed_at DATETIME,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_owner (owner),
    INDEX idx_district (district),
    INDEX idx_status (status),
    INDEX idx_created (created_at)
);

-- 地区表
CREATE TABLE IF NOT EXISTS districts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL UNIQUE,
    sort_order INT DEFAULT 0
);

-- 初始化地区数据
INSERT INTO districts (name, sort_order) VALUES ('北京市-奶东村', 1);