-- Finances Agent 数据库初始化
CREATE DATABASE IF NOT EXISTS finances DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE finances;

-- 家庭成员表
CREATE TABLE IF NOT EXISTS family_members (
 id INT AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(50) NOT NULL,
 role VARCHAR(20) COMMENT '配偶/子女/父母/其他',
 is_active BOOLEAN DEFAULT TRUE,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 收入记录表
CREATE TABLE IF NOT EXISTS income_records (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 source VARCHAR(100) COMMENT '工资/奖金/副业/投资收益',
 amount DECIMAL(12,2) NOT NULL,
 frequency VARCHAR(20) COMMENT '月/季/年/一次性',
 recorded_date DATE,
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 固定支出表
CREATE TABLE IF NOT EXISTS fixed_expenses (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 category VARCHAR(50) COMMENT '房贷/房租/车贷/保险/物业费/学费',
 amount DECIMAL(12,2) NOT NULL,
 frequency VARCHAR(20) COMMENT '月/季/年',
 due_date INT COMMENT '扣款日(1-31)',
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 可变支出表
CREATE TABLE IF NOT EXISTS variable_expenses (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 category VARCHAR(50) COMMENT '餐饮/购物/娱乐/交通/医疗/人情',
 amount DECIMAL(12,2) NOT NULL,
 recorded_date DATE,
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 资产表
CREATE TABLE IF NOT EXISTS assets (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 asset_type VARCHAR(50) COMMENT '存款/理财/基金/股票/房产/车辆',
 name VARCHAR(100),
 current_value DECIMAL(12,2) NOT NULL,
 note TEXT,
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 负债表
CREATE TABLE IF NOT EXISTS liabilities (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 liability_type VARCHAR(50) COMMENT '房贷/车贷/消费贷/信用卡/亲友借款',
 total_amount DECIMAL(12,2) NOT NULL,
 interest_rate DECIMAL(5,2) COMMENT '年利率%',
 remaining_months INT,
 monthly_payment DECIMAL(12,2),
 note TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 财务目标表
CREATE TABLE IF NOT EXISTS financial_goals (
 id INT AUTO_INCREMENT PRIMARY KEY,
 member_id INT,
 goal_type VARCHAR(20) COMMENT '短期/中期/长期',
 description VARCHAR(200) NOT NULL,
 target_amount DECIMAL(12,2),
 target_date DATE,
 current_progress DECIMAL(12,2) DEFAULT 0,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (member_id) REFERENCES family_members(id)
);

-- 对话日志表
CREATE TABLE IF NOT EXISTS conversation_log (
 id INT AUTO_INCREMENT PRIMARY KEY,
 user_input TEXT,
 agent_response TEXT,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
