# The Mia! Accounting Project.
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/1/25

#  Copyright (c) 2023 imacat.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
"""The console commands for the base account management.

"""
import csv

import sqlalchemy as sa

from accounting import data_dir
from accounting import db
from accounting.models import BaseAccount, BaseAccountL10n
from accounting.utils.title_case import title_case


def init_base_accounts_command() -> None:
    """Initializes the base accounts."""
    if BaseAccount.query.first() is not None:
        return

    with open(data_dir / "base_accounts.csv") as fp:
        data: list[dict[str, str]] = [x for x in csv.DictReader(fp)]
    account_data: list[dict[str, str]] = [{"code": x["code"],
                                           "title_l10n": title_case(x["title"])}
                                          for x in data]
    locales: list[str] = [x[5:] for x in data[0] if x.startswith("l10n-")]
    l10n_data: list[dict[str, str]] = [{"account_code": x["code"],
                                        "locale": y,
                                        "title": x[f"l10n-{y}"]}
                                       for x in data for y in locales]
    db.session.execute(sa.insert(BaseAccount), account_data)
    db.session.execute(sa.insert(BaseAccountL10n), l10n_data)
