#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Recalcula campos compute de skeen.cita tras importación masiva."""

import sys
sys.path.insert(0, '/root/skeen-odoo')

import odoo
from odoo.tools import config

config.parse_config([
    '-c', '/root/skeen-odoo/odoo.conf',
    '--no-http',
    '--log-level', 'error',
])

odoo.service.server.load_server_wide_modules()
registry = odoo.registry(config['db_name'])

with registry.cursor() as cr:
    env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
    Citas = env['skeen.cita']
    total = Citas.search_count([])
    print(f'Total citas: {total}')

    # Recalcular campos compute para todas las citas
    todas = Citas.search([])
    todas._compute_datetime()
    todas._compute_time_str()
    cr.commit()

    nulls = Citas.search_count([('datetime_start', '=', False)])
    print(f'Citas con datetime_start NULL tras recálculo: {nulls}')

print('Recálculo completado.')
