# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name                 : Mascaret
Description          : Pre and Postprocessing for Mascaret for QGIS
Date                 : Aprile, 2025
copyright            : (C) 2025 by Artelia
email                :
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""
from lib.db import MasObject as Maso


class ClassUpdate700:

    def __init__(self, check_tab):
        self.mdb = check_tab.mdb
        self.mgis = check_tab.mgis
        self.cht = check_tab

    def update700(self):
        self.mgis.add_info("*** Update 7.0.0  ***")
        tabs = self.mdb.list_tables(self.mdb.SCHEMA)
        lst_add_tab = [
            "assim_config",
            "assim_ks",
            "assim_law",
            "assim_res",
            "assim_res_ks",
            "assim_res_law",
        ]
        valide = True
        for attr in lst_add_tab:
            if attr not in tabs:
                valid_add, _ = self.cht.add_tab(getattr(Maso, attr))
                if not valid_add:
                    self.mgis.add_info(f"Create  the {attr} table - ERROR")
                    valide = False

        lst_alt = [
            "ALTER TABLE {schema}.outputs ADD COLUMN "
            "IF NOT EXISTS obsZ_stdErr DOUBLE PRECISION DEFAULT 0.05;",
            "ALTER TABLE {schema}.outputs ADD COLUMN "
            "IF NOT EXISTS obsQ_stdErr DOUBLE PRECISION DEFAULT 10;",
            "ALTER TABLE {schema}.outputs ADD COLUMN "
            "IF NOT EXISTS obsQ_rejectLimit DOUBLE PRECISION DEFAULT NULL;",
            "ALTER TABLE {schema}.outputs ADD COLUMN "
            "IF NOT EXISTS obsZ_rejectLimit DOUBLE PRECISION DEFAULT NULL;",
        ]
        # Alter colonne value en text
        for sql in lst_alt:
            try:
                self.mdb.execute(sql, schema=True)
            except Exception:
                self.mgis.add_info("Alter  the output table - ERROR")
                valide = False
        return valide
