const request = require('supertest');
const app = require('../final-backend');

describe('采购付款分离 - API层测试', () => {
  describe('采购申请API', () => {
    let testPurchaseRequestId;

    test('POST /api/purchase-requests - 应该能创建采购申请', async () => {
      const response = await request(app)
        .post('/api/purchase-requests')
        .send({
          project_id: 1,
          applicant: '测试申请人',
          request_date: '2026-03-25',
          expense_category: 'material',
          total_amount: 15000,
          items: [
            {
              product_name: '测试商品A',
              quantity: 10,
              unit_price: 1000,
              total_price: 10000
            },
            {
              product_name: '测试商品B',
              quantity: 5,
              unit_price: 1000,
              total_price: 5000
            }
          ]
        });
      
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('id');
      expect(response.body.data).toHaveProperty('request_code');
      testPurchaseRequestId = response.body.data.id;
    });

    test('GET /api/purchase-requests - 应该能获取采购申请列表', async () => {
      const response = await request(app).get('/api/purchase-requests');
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(Array.isArray(response.body.data)).toBe(true);
    });

    test('GET /api/purchase-requests/:id - 应该能获取采购申请详情', async () => {
      const response = await request(app).get(`/api/purchase-requests/${testPurchaseRequestId}`);
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('items');
      expect(response.body.data.items.length).toBe(2);
    });

    test('POST /api/purchase-requests/:id/submit - 应该能提交采购申请', async () => {
      const response = await request(app)
        .post(`/api/purchase-requests/${testPurchaseRequestId}/submit`);
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
    });

    test('POST /api/purchase-requests/:id/approve - 应该能审批通过采购申请', async () => {
      const response = await request(app)
        .post(`/api/purchase-requests/${testPurchaseRequestId}/approve`);
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
    });

    test('PUT /api/purchase-requests/:id - 应该能更新采购申请', async () => {
      const response = await request(app)
        .put(`/api/purchase-requests/${testPurchaseRequestId}`)
        .send({
          project_id: 1,
          applicant: '测试申请人',
          request_date: '2026-03-25',
          expense_category: 'material',
          total_amount: 20000,
          remark: '更新备注'
        });
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
    });
  });

  describe('库存管理API', () => {
    test('GET /api/inventory - 应该能获取库存记录', async () => {
      const response = await request(app).get('/api/inventory');
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(Array.isArray(response.body.data)).toBe(true);
    });

    test('GET /api/inventory/summary - 应该能获取库存汇总', async () => {
      const response = await request(app).get('/api/inventory/summary');
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(Array.isArray(response.body.data)).toBe(true);
    });

    test('POST /api/inventory/out - 应该能创建出库记录', async () => {
      const response = await request(app)
        .post('/api/inventory/out')
        .send({
          project_id: 1,
          product_id: 1,
          quantity: 10,
          operator: '测试操作员'
        });
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
    });
  });

  describe('项目成本统计API', () => {
    test('GET /api/projects/:id/cost-summary - 应该能获取项目成本统计', async () => {
      const response = await request(app).get('/api/projects/1/cost-summary');
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
      expect(response.body.data).toHaveProperty('project_name');
      expect(response.body.data).toHaveProperty('purchase_cost');
      expect(response.body.data).toHaveProperty('payment_cost');
      expect(response.body.data).toHaveProperty('total_cost');
      expect(response.body.data).toHaveProperty('profit');
    });
  });

  describe('采购申请删除API', () => {
    let tempPurchaseRequestId;

    beforeAll(async () => {
      const response = await request(app)
        .post('/api/purchase-requests')
        .send({
          project_id: 1,
          applicant: '临时测试',
          request_date: '2026-03-25',
          expense_category: 'material',
          total_amount: 1000
        });
      tempPurchaseRequestId = response.body.data.id;
    });

    test('DELETE /api/purchase-requests/:id - 应该能删除采购申请', async () => {
      const response = await request(app)
        .delete(`/api/purchase-requests/${tempPurchaseRequestId}`);
      expect(response.status).toBe(200);
      expect(response.body.success).toBe(true);
    });

    test('GET /api/purchase-requests/:id - 删除后应该返回404', async () => {
      const response = await request(app)
        .get(`/api/purchase-requests/${tempPurchaseRequestId}`);
      expect(response.status).toBe(404);
    });
  });
});
