import json
import logging

from odoo import http
from odoo.http import request

_logger = logging.getLogger(__name__)


def _json_body():
    try:
        return json.loads(request.httprequest.data or '{}')
    except Exception:
        return {}


def _json_response(data, status=200):
    return request.make_json_response(data, status=status)


class ExamStructureController(http.Controller):

    @http.route('/api/exam-structures', type='http', auth='user', methods=['GET'], csrf=False)
    def list_structures(self, **kw):
        domain = [('active', '=', True)]
        entity_id = kw.get('entity_id')
        if entity_id:
            domain.append(('entity_id', '=', int(entity_id)))

        limit = int(kw.get('limit', 50))
        offset = int(kw.get('offset', 0))
        records = request.env['encoach.exam.structure'].search(domain, limit=limit, offset=offset, order='create_date desc')
        total = request.env['encoach.exam.structure'].search_count(domain)

        items = []
        for r in records:
            modules = []
            if r.modules:
                try:
                    modules = json.loads(r.modules)
                except Exception:
                    modules = []
            items.append({
                'id': r.id,
                'name': r.name,
                'entity_id': r.entity_id.id if r.entity_id else None,
                'entity_name': r.entity_id.name if r.entity_id else None,
                'industry': r.industry or '',
                'modules': modules,
                'config': json.loads(r.config) if r.config else {},
            })

        return _json_response({'items': items, 'total': total})

    @http.route('/api/exam-structures', type='http', auth='user', methods=['POST'], csrf=False)
    def create_structure(self, **kw):
        body = _json_body()
        name = body.get('name')
        if not name:
            return _json_response({'error': 'name is required'}, status=400)

        vals = {
            'name': name,
            'industry': body.get('industry', ''),
            'modules': json.dumps(body.get('modules', [])),
            'config': json.dumps(body.get('config', {})),
        }
        entity_id = body.get('entity_id')
        if entity_id:
            vals['entity_id'] = int(entity_id)

        record = request.env['encoach.exam.structure'].create(vals)
        return _json_response({
            'id': record.id,
            'name': record.name,
            'entity_id': record.entity_id.id if record.entity_id else None,
            'industry': record.industry or '',
            'modules': json.loads(record.modules) if record.modules else [],
        })

    @http.route('/api/exam-structures/<int:structure_id>', type='http', auth='user', methods=['DELETE'], csrf=False)
    def delete_structure(self, structure_id, **kw):
        record = request.env['encoach.exam.structure'].browse(structure_id)
        if not record.exists():
            return _json_response({'error': 'Structure not found'}, status=404)
        record.unlink()
        return _json_response({'success': True})
