import pytest
import json
import os

class TestSchemas:
    """Test schema management functionality."""
    
    def test_list_schemas(self, logged_in_client):
        """Test listing schemas."""
        response = logged_in_client.get('/schemas/')
        assert response.status_code == 200
        assert b'Esquemas de Proyecto' in response.data
        
    def test_view_schema(self, logged_in_client):
        """Test viewing a schema."""
        response = logged_in_client.get('/schemas/view/TEST001')
        assert response.status_code == 200
        assert b'Esquema de prueba' in response.data
        
    def test_create_schema(self, logged_in_client, app):
        """Test creating a new schema."""
        response = logged_in_client.post('/schemas/create', data={
            'codigo': 'TEST002',
            'descripcion': 'Nuevo esquema de prueba',
            'documentos-0-tipo': 'pdf',
            'documentos-0-nombre': 'Manual de Usuario',
            'documentos-0-nivel_ver': 0,
            'documentos-0-nivel_editar': 5000,
            'documentos-1-tipo': 'txt',
            'documentos-1-nombre': 'Notas de Proyecto',
            'documentos-1-nivel_ver': 0,
            'documentos-1-nivel_editar': 1000
        }, follow_redirects=True)
        
        assert response.status_code == 200
        
        # Check if schema was created
        with app.app_context():
            schemas_path = os.path.join(app.config['STORAGE_PATH'], 'schemas', 'schema.json')
            with open(schemas_path, 'r') as f:
                schemas = json.load(f)
                assert 'TEST002' in schemas
                assert schemas['TEST002']['descripcion'] == 'Nuevo esquema de prueba'
                assert len(schemas['TEST002']['documentos']) == 2
                
    def test_edit_schema(self, logged_in_client, app):
        """Test editing a schema."""
        # First create a schema to edit
        logged_in_client.post('/schemas/create', data={
            'codigo': 'TESTEDIT',
            'descripcion': 'Esquema para editar',
            'documentos-0-tipo': 'pdf',
            'documentos-0-nombre': 'Documento Original',
            'documentos-0-nivel_ver': 0,
            'documentos-0-nivel_editar': 5000
        }, follow_redirects=True)
        
        # Now edit it
        response = logged_in_client.post('/schemas/edit/TESTEDIT', data={
            'codigo': 'TESTEDIT',
            'descripcion': 'Esquema editado',
            'documentos-0-tipo': 'pdf',
            'documentos-0-nombre': 'Documento Modificado',
            'documentos-0-nivel_ver': 500,
            'documentos-0-nivel_editar': 6000
        }, follow_redirects=True)
        
        assert response.status_code == 200
        
        # Verify changes
        with app.app_context():
            schemas_path = os.path.join(app.config['STORAGE_PATH'], 'schemas', 'schema.json')
            with open(schemas_path, 'r') as f:
                schemas = json.load(f)
                assert 'TESTEDIT' in schemas
                assert schemas['TESTEDIT']['descripcion'] == 'Esquema editado'
                assert schemas['TESTEDIT']['documentos'][0]['nombre'] == 'Documento Modificado'
                assert schemas['TESTEDIT']['documentos'][0]['nivel_ver'] == 500
