# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 XiVO Solutions
#
# 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 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>

from xivo_dao.alchemy.ctidirectories import CtiDirectories
from xivo_dao.alchemy.ctidirectories_by_labels import CtiDirectoriesByLabels
from xivo_dao.alchemy.ctidirectories_default_sources import CtiDirectoriesDefaultSources
from xivo_dao.alchemy.ctidirectories_label_mapping import CtiDirectoriesLabelMapping
from xivo_dao.alchemy.labels import Labels
from xivo_dao.helpers.db_manager import Session


def get():
    return Session.query(CtiDirectoriesByLabels).first()


def set_display(display_id):
    Session.query(CtiDirectoriesByLabels).update({'display_id': display_id})
    Session.flush()


def find_all_default_sources():
    return Session.query(CtiDirectoriesDefaultSources).all()


def add_default_source(source_id):
    source = CtiDirectoriesDefaultSources(source_id=source_id)
    Session.add(source)
    Session.flush()
    return source


def remove_default_source(source_id):
    Session.query(CtiDirectoriesDefaultSources).filter_by(
        source_id=source_id
    ).delete()
    Session.flush()


def find_all_mappings():
    return Session.query(CtiDirectoriesLabelMapping).all()


def associate(directory, label):
    mapping = CtiDirectoriesLabelMapping(
        source_id=directory.id, label_id=label.id
    )
    Session.add(mapping)
    Session.flush()
    return mapping


def dissociate(directory, label):
    Session.query(CtiDirectoriesLabelMapping).filter_by(
        source_id=directory.id, label_id=label.id
    ).delete()
    Session.flush()


def get_lookup_by_label_config():
    config = get()
    display_id = config.display_id if config else None

    default_sources = [
        name
        for (name,) in Session.query(CtiDirectories.name).join(
            CtiDirectoriesDefaultSources,
            CtiDirectoriesDefaultSources.source_id == CtiDirectories.id,
        )
    ]

    labels = {}
    mappings = (
        Session.query(
            CtiDirectoriesLabelMapping.label_id,
            Labels.display_name,
            CtiDirectories.name,
        )
        .join(Labels, Labels.id == CtiDirectoriesLabelMapping.label_id)
        .join(
            CtiDirectories,
            CtiDirectories.id == CtiDirectoriesLabelMapping.source_id,
        )
    )
    for label_id, label_name, source_name in mappings:
        entry = labels.setdefault(label_id, {'name': label_name, 'sources': []})
        entry['sources'].append(source_name)

    return {
        'display_id': display_id,
        'default_sources': default_sources,
        'labels': labels,
    }


def get_display_name():
    config = get()
    return config.display.name if config else None
