package com.addd.xingdongli.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    // 数据库名称及版本号
    private static final String DATABASE_NAME = "xingdongli.db";
    private static final int DATABASE_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // 当数据库第一次被创建时调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建用户信息表
        db.execSQL("CREATE TABLE IF NOT EXISTS user_info (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "name TEXT, " +
                "gender TEXT, " +
                "age INTEGER, " +
                "height REAL, " +
                "weight REAL, " +
                "goal TEXT, " +
                "created_at TEXT" +
                ")");

        // 创建训练计划表
        db.execSQL("CREATE TABLE IF NOT EXISTS training_plan (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "date TEXT, " +
                "action_name TEXT, " +
                "duration_minutes INTEGER, " +
                "intensity TEXT, " +
                "user_id INTEGER" +
                ")");

        // 创建训练记录表
        db.execSQL("CREATE TABLE IF NOT EXISTS training_record (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "date TEXT, " +
                "action_name TEXT, " +
                "score INTEGER, " +
                "notes TEXT, " +
                "user_id INTEGER" +
                ")");

        // 创建姿态评分表
        db.execSQL("CREATE TABLE IF NOT EXISTS pose_score (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "record_id INTEGER, " +
                "keypoint_accuracy REAL, " +
                "overall_score INTEGER, " +
                "suggestions TEXT" +
                ")");

        // 创建视频历史记录表
        db.execSQL("CREATE TABLE IF NOT EXISTS video_history (" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "filepath TEXT, " +
                "upload_date TEXT, " +
                "linked_record_id INTEGER" +
                ")");
    }

    // 当数据库版本升级时调用，这里可以做数据迁移
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 简单示例：删除旧表，然后重新创建（生产环境中建议做更细粒度的数据迁移）
        db.execSQL("DROP TABLE IF EXISTS user_info");
        db.execSQL("DROP TABLE IF EXISTS training_plan");
        db.execSQL("DROP TABLE IF EXISTS training_record");
        db.execSQL("DROP TABLE IF EXISTS pose_score");
        db.execSQL("DROP TABLE IF EXISTS video_history");
        onCreate(db);
    }
}
