import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { buildApp } from '../../app.js';
describe('team routes', () => {
    let app;
    let cookie;
    beforeAll(async () => {
        app = await buildApp({ config: { jwtSecret: 'test-secret-team' } });
        await app.ready();
        // Login as default admin
        const loginRes = await app.inject({
            method: 'POST',
            url: '/api/auth/login',
            payload: { username: 'admin', password: 'admin' },
        });
        const cookies = loginRes.cookies;
        const access = cookies.find((c) => c.name === 'access_token');
        const refresh = cookies.find((c) => c.name === 'refresh_token');
        cookie = `access_token=${access.value}; refresh_token=${refresh.value}`;
        // Clean table from previous runs
        app.db.exec('DELETE FROM team_members');
    });
    afterAll(async () => {
        await app.close();
    });
    it('GET /api/team — returns empty list initially', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/team',
            headers: { cookie },
        });
        expect(res.statusCode).toBe(200);
        const body = res.json();
        expect(body.members).toEqual([]);
    });
    it('POST /api/team — creates a team member', async () => {
        const res = await app.inject({
            method: 'POST',
            url: '/api/team',
            headers: { cookie },
            payload: {
                name: 'Maria Silva',
                role: 'Designer',
                email: 'maria@example.com',
                phone: '+55 11 99999-0000',
            },
        });
        expect(res.statusCode).toBe(201);
        const body = res.json();
        expect(body.member.name).toBe('Maria Silva');
        expect(body.member.role).toBe('Designer');
        expect(body.member.email).toBe('maria@example.com');
        expect(body.member.phone).toBe('+55 11 99999-0000');
        expect(body.member.id).toMatch(/^tm-/);
    });
    it('POST /api/team — rejects missing name', async () => {
        const res = await app.inject({
            method: 'POST',
            url: '/api/team',
            headers: { cookie },
            payload: { role: 'Dev' },
        });
        expect(res.statusCode).toBe(400);
    });
    it('PATCH /api/team/:id — updates a team member', async () => {
        // Create first
        const createRes = await app.inject({
            method: 'POST',
            url: '/api/team',
            headers: { cookie },
            payload: { name: 'Joao', role: 'Dev' },
        });
        const { member } = createRes.json();
        const res = await app.inject({
            method: 'PATCH',
            url: `/api/team/${member.id}`,
            headers: { cookie },
            payload: { role: 'Senior Dev', email: 'joao@test.com' },
        });
        expect(res.statusCode).toBe(200);
        const updated = res.json().member;
        expect(updated.name).toBe('Joao');
        expect(updated.role).toBe('Senior Dev');
        expect(updated.email).toBe('joao@test.com');
    });
    it('DELETE /api/team/:id — deletes a team member', async () => {
        const createRes = await app.inject({
            method: 'POST',
            url: '/api/team',
            headers: { cookie },
            payload: { name: 'Temp User', role: 'Intern' },
        });
        const { member } = createRes.json();
        const res = await app.inject({
            method: 'DELETE',
            url: `/api/team/${member.id}`,
            headers: { cookie },
        });
        expect(res.statusCode).toBe(200);
        expect(res.json().ok).toBe(true);
        // Verify deleted
        const delRes = await app.inject({
            method: 'DELETE',
            url: `/api/team/${member.id}`,
            headers: { cookie },
        });
        expect(delRes.statusCode).toBe(404);
    });
    it('rejects unauthenticated requests', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/team',
        });
        expect(res.statusCode).toBe(401);
    });
});
//# sourceMappingURL=team-routes.test.js.map