-- +migrate Up

CREATE TABLE photos (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    filename VARCHAR(255) NOT NULL,
    file_path VARCHAR(500) NOT NULL,
    file_size BIGINT,
    mime_type VARCHAR(100),
    width INTEGER,
    height INTEGER,
    category_id INTEGER REFERENCES categories(id),
    exif JSONB,
    taken_at TIMESTAMP,
    location VARCHAR(255),
    is_public BOOLEAN DEFAULT true,
    status VARCHAR(20) DEFAULT 'draft',
    view_count INTEGER DEFAULT 0,
    like_count INTEGER DEFAULT 0,
    user_id INTEGER NOT NULL REFERENCES users(id),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 创建照片标签关联表
CREATE TABLE photo_tags (
    photo_id INTEGER REFERENCES photos(id) ON DELETE CASCADE,
    tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE,
    PRIMARY KEY (photo_id, tag_id)
);

-- 创建索引
CREATE INDEX idx_photos_category_id ON photos(category_id);
CREATE INDEX idx_photos_user_id ON photos(user_id);
CREATE INDEX idx_photos_status ON photos(status);
CREATE INDEX idx_photos_is_public ON photos(is_public);
CREATE INDEX idx_photos_taken_at ON photos(taken_at);
CREATE INDEX idx_photos_created_at ON photos(created_at);
CREATE INDEX idx_photos_view_count ON photos(view_count);
CREATE INDEX idx_photos_like_count ON photos(like_count);
CREATE INDEX idx_photos_deleted_at ON photos(deleted_at);

-- 为JSONB字段创建GIN索引（支持高效的JSON查询）
CREATE INDEX idx_photos_exif_gin ON photos USING GIN (exif);

-- 全文搜索索引
CREATE INDEX idx_photos_title_gin ON photos USING GIN (to_tsvector('english', title));
CREATE INDEX idx_photos_description_gin ON photos USING GIN (to_tsvector('english', description));

-- +migrate Down

DROP TABLE IF EXISTS photo_tags;
DROP TABLE IF EXISTS photos;