import { describe, it, expect, beforeAll, afterAll } from '@jest/globals';

const API_BASE = process.env.TEST_API_URL || 'http://localhost:3000/api';
let authToken: string;
let testUserId: string;

describe('Veridian API Tests', () => {
    describe('Health Check', () => {
        it('should return ok status', async () => {
            const response = await fetch(`${API_BASE}/healthz`);
            const data = await response.json() as { status: string; timestamp: string };

            expect(response.status).toBe(200);
            expect(data.status).toBe('ok');
            expect(data.timestamp).toBeDefined();
        });
    });

    describe('Auth Routes', () => {
        describe('POST /auth/login', () => {
            it('should reject invalid credentials', async () => {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        email: 'nonexistent@test.com',
                        password: 'wrongpassword'
                    })
                });

                expect(response.status).toBe(401);
            });

            it('should require email and password', async () => {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({})
                });

                expect(response.status).toBe(400);
            });

            it('should login with valid credentials', async () => {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        email: 'admin@veridian.app',
                        password: 'admin123'
                    })
                });

                if (response.status === 200) {
                    const data = await response.json() as { token: string; user?: { id: string } };
                    expect(data.token).toBeDefined();
                    authToken = data.token;
                    testUserId = data.user?.id || '';
                }
            });
        });
    });

    describe('Protected Routes', () => {
        it('should reject requests without auth token', async () => {
            const response = await fetch(`${API_BASE}/rooms`);
            expect(response.status).toBe(401);
        });

        it('should reject requests with invalid token', async () => {
            const response = await fetch(`${API_BASE}/rooms`, {
                headers: { 'Authorization': 'Bearer invalid-token' }
            });
            expect(response.status).toBe(401);
        });
    });

    describe('Rooms API', () => {
        it('should list rooms with valid token', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/rooms`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json();
            expect(Array.isArray(data)).toBe(true);
        });
    });

    describe('Batches API', () => {
        it('should list batches with valid token', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/batches`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json();
            expect(Array.isArray(data)).toBe(true);
        });
    });

    describe('Tasks API', () => {
        it('should list tasks with valid token', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/tasks`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Supplies API', () => {
        it('should list supplies with valid token', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/supplies`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Timeclock API', () => {
        it('should get active entry', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/timeclock/active`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect([200, 404]).toContain(response.status);
        });
    });

    describe('Walkthrough API', () => {
        it('should list walkthroughs', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/walkthrough`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Upload API', () => {
        it('should return upload stats', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/upload/stats`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json() as { sizes: unknown };
            expect(data.sizes).toBeDefined();
        });
    });

    describe('Environment API', () => {
        it('should get environment dashboard', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/environment/dashboard`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });

        it('should list sensors', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/environment/sensors`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json();
            expect(Array.isArray(data)).toBe(true);
        });
    });

    describe('Financial API', () => {
        it('should get transactions', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/financial/transactions`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json() as { transactions: unknown[] };
            expect(data.transactions).toBeDefined();
        });

        it('should get profit/loss report', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/financial/reports/profit-loss`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Insights API', () => {
        it('should get insights dashboard', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/insights/dashboard`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });

        it('should list anomalies', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/insights/anomalies`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
            const data = await response.json();
            expect(Array.isArray(data)).toBe(true);
        });
    });

    describe('Visitors API', () => {
        it('should list visitors', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/visitors`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });

        it('should get active visitors', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/visitors/active`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Messaging API', () => {
        it('should list announcements', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/messaging/announcements`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Audit API', () => {
        it('should get audit logs', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/audit/logs`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });

    describe('Documents API', () => {
        it('should list documents', async () => {
            if (!authToken) return;

            const response = await fetch(`${API_BASE}/documents`, {
                headers: { 'Authorization': `Bearer ${authToken}` }
            });

            expect(response.status).toBe(200);
        });
    });
});
