const db = require('../db-sqlite');
const path = require('path');

describe('采购付款分离 - 数据库层测试', () => {
  beforeAll(async () => {
    await new Promise(resolve => setTimeout(resolve, 1000));
  });

  describe('采购申请表 (purchase_requests)', () => {
    test('应该能创建采购申请', async () => {
      const result = await db.query(`
        INSERT INTO purchase_requests 
        (request_code, project_id, applicant, request_date, expense_category, total_amount, status)
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `, ['PUR-TEST-001', 1, '测试申请人', '2026-03-25', 'material', 10000.00, 'pending']);
      expect(result.changes).toBe(1);
    });

    test('采购申请编号应该唯一', async () => {
      try {
        await db.query(`
          INSERT INTO purchase_requests 
          (request_code, project_id, applicant, request_date, expense_category, total_amount, status)
          VALUES (?, ?, ?, ?, ?, ?, ?)
        `, ['PUR-TEST-001', 1, '测试申请人2', '2026-03-25', 'material', 20000.00, 'pending']);
        fail('应该抛出唯一约束错误');
      } catch (error) {
        expect(error.message).toContain('UNIQUE constraint failed');
      }
    });

    test('应该能查询采购申请', async () => {
      const result = await db.query('SELECT * FROM purchase_requests WHERE request_code = ?', ['PUR-TEST-001']);
      expect(result.length).toBeGreaterThan(0);
      expect(result[0].request_code).toBe('PUR-TEST-001');
    });
  });

  describe('采购明细表 (purchase_request_items)', () => {
    let purchaseRequestId;

    beforeAll(async () => {
      const result = await db.query(`
        INSERT INTO purchase_requests 
        (request_code, project_id, applicant, request_date, expense_category, total_amount, status)
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `, ['PUR-TEST-002', 1, '测试申请人', '2026-03-25', 'material', 5000.00, 'pending']);
      purchaseRequestId = result.lastID;
    });

    test('应该能创建采购明细', async () => {
      const result = await db.query(`
        INSERT INTO purchase_request_items 
        (purchase_request_id, product_name, quantity, unit_price, total_price)
        VALUES (?, ?, ?, ?, ?)
      `, [purchaseRequestId, '测试商品', 10, 500.00, 5000.00]);
      expect(result.changes).toBe(1);
    });

    test('应该能查询采购明细', async () => {
      const result = await db.query('SELECT * FROM purchase_request_items WHERE purchase_request_id = ?', [purchaseRequestId]);
      expect(result.length).toBeGreaterThan(0);
      expect(result[0].product_name).toBe('测试商品');
    });
  });

  describe('库存记录表 (inventory_records)', () => {
    test('应该能创建库存入库记录', async () => {
      const result = await db.query(`
        INSERT INTO inventory_records 
        (record_type, product_id, quantity, unit_price, total_amount, record_date, operator)
        VALUES (?, ?, ?, ?, ?, ?, ?)
      `, ['in', 1, 100, 10.00, 1000.00, '2026-03-25', '测试操作员']);
      expect(result.changes).toBe(1);
    });

    test('应该能创建库存出库记录', async () => {
      const result = await db.query(`
        INSERT INTO inventory_records 
        (record_type, project_id, product_id, quantity, unit_price, total_amount, record_date, operator)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
      `, ['out', 1, 1, 50, 10.00, 500.00, '2026-03-25', '测试操作员']);
      expect(result.changes).toBe(1);
    });
  });

  describe('付款申请表修改', () => {
    test('付款申请表应该有purchase_request_id字段', async () => {
      const result = await db.query("PRAGMA table_info(payment_requests)");
      const hasPurchaseRequestId = result.some(col => col.name === 'purchase_request_id');
      expect(hasPurchaseRequestId).toBe(true);
    });

    test('付款申请表应该有payment_type字段', async () => {
      const result = await db.query("PRAGMA table_info(payment_requests)");
      const hasPaymentType = result.some(col => col.name === 'payment_type');
      expect(hasPaymentType).toBe(true);
    });
  });
});
