from SQLiteInterface import SQLiteInterface

# 主程序入口，进行数据库表的创建和数据插入
if __name__ == "__main__":
    db_interface = SQLiteInterface()

    # SQL语句：创建ActivityCategories表
    create_table_sql = '''
    CREATE TABLE IF NOT EXISTS ActivityCategories (
    activity_id INTEGER PRIMARY KEY AUTOINCREMENT,
    activity_name TEXT NOT NULL UNIQUE,
    category TEXT NOT NULL CHECK (category IN ('Operating', 'Investing', 'Financing')),
    direction TEXT NOT NULL CHECK (direction IN ('Inflow', 'Outflow')),
    description TEXT,
    parent_category_id INTEGER,
    FOREIGN KEY (parent_category_id) REFERENCES ActivityCategories (activity_id)
    );
    '''

    # SQL语句：插入示例数据到ActivityCategories表
    insert_data_sql = '''
    INSERT INTO ActivityCategories (activity_name, category, direction, description, parent_category_id) VALUES
    -- 经营活动
    ('SalaryIncome', 'Operating', 'Inflow', '提供劳务收到的现金', NULL),
    ('GoodIncome', 'Operating', 'Inflow', '销售边际成本为零的产品（杠杆品）收到的现金', NULL),
    ('OtherIncome', 'Operating', 'Inflow', '意外收到的现金', NULL),
    ('NoneIncome', 'Operating', 'Inflow', '未消费收到的现金退款', NULL),
    ('FixedExpenses', 'Operating', 'Outflow', '必须且固定的现金支出', NULL),
    ('DailyExpenses', 'Operating', 'Outflow', '必须但不固定的现金支出', NULL),
    ('PersonalizedExpenses', 'Operating', 'Outflow', '不必须的现金支出', NULL),
    -- 投资活动
    ('InvestmentLiquidation', 'Investing', 'Inflow', '收回投资收到的现金', NULL),
    ('InvestmentReturns', 'Investing', 'Inflow', '取得投资收益收到的现金', NULL),
    ('CashflowFromCapEx', 'Investing', 'Inflow', '处置固定资产、无形资产和其他长期资产收回的现金净额', NULL),
    ('DefensiveAssets', 'Investing', 'Outflow', '购建防守资产支付的现金', NULL),
    ('StableAssets', 'Investing', 'Outflow', '购建稳健资产支付的现金', NULL),
    ('AggressiveAssets', 'Investing', 'Outflow', '购建进攻资产支付的现金', NULL),
    ('PerpetualAssets', 'Investing', 'Outflow', '购建永续资产支付的现金', NULL),
    ('CapitalExpenditures', 'Investing', 'Outflow', '购建固定资产、无形资产和其他长期资产支付的现金', NULL),
    ('USInvestmentLiquidation', 'Investing', 'Inflow', '收回投资收到的美元/港币现金', NULL),
    ('USInvestmentReturns', 'Investing', 'Inflow', '取得投资收益收到的美元/港币现金', NULL),
    ('USStableAssets', 'Investing', 'Outflow', '购建稳健资产支付的美元/港币现金', NULL),
    ('USAggressiveAssets', 'Investing', 'Outflow', '购建进攻资产支付的美元/港币现金', NULL),
    -- 融资活动 
    ('ProceedsfromIssuingShares', 'Financing', 'Inflow', '吸收投资收到的现金', NULL),
    ('ProceedsfromBorrowings', 'Financing', 'Inflow', '取得借款收到的现金', NULL),
    ('ShortTermLoans', 'Financing', 'Outflow', '短期债务', NULL),
    ('DebtRepayments', 'Financing', 'Outflow', '中长期债务', NULL),
    ('FamilyFunds', 'Financing', 'Outflow', '家庭共同基金', NULL);
    '''
    
    try:
        # 尝试执行创建表的SQL语句
        db_interface.run_sql(create_table_sql)
        print("表创建成功。")

        # 尝试执行插入数据的SQL语句
        db_interface.run_sql(insert_data_sql)
        print("数据插入成功。")

    except Exception as e:
        # 捕获并打印所有其他未预期的错误
        print(f"操作失败: {e}")

