import logging
from django.conf import settings
from django.core.exceptions import ValidationError

from apps.companies.repositories import (
    CompanyInfoRepository,
    CompanyRepository
)


logger = logging.getLogger(__name__)


class CompanyContextService:

    @staticmethod
    def get_context(company_name: str = None) -> dict:
        static_context = CompanyContextService.get_static_context()
        if not company_name:
            return static_context

        try:
            company_info = CompanyInfoRepository.get_company_info(
                company_name=company_name
            )

            if company_info:
                return {
                    "company_name": company_info.company_name,
                    "company_address": company_info.company_address,
                    "company_email": company_info.company_email,
                    "company_phone": company_info.company_phone,
                    "company_website": company_info.company_website,
                    "company_logo": company_info.company_logo.url,
                    "text_color": company_info.company_text_color,
                    "primary_color": company_info.company_primary_color,
                    "secondary_color": company_info.company_secondary_color,
                    "copyright": company_info.company_copyright,
                    "button_color": company_info.company_button_color,
                    "starting_year": company_info.company_starting_year,
                }

            return static_context
        except Exception as exc:
            logger.warning(f"CompanyContextService get_context {exc}")
            return static_context

    @staticmethod
    def get_static_context() -> dict:

        return {
            "company_name": settings.COMPANY_NAME,
            "company_address": settings.COMPANY_ADDRESS,
            "company_email": settings.COMPANY_EMAIL,
            "company_phone": settings.COMPANY_PHONE,
            "company_website": settings.COMPANY_WEBSITE,
            "company_logo": settings.COMPANY_LOGO,

            "text_color": settings.TEXT_COLOR or "#111827",
            "primary_color": settings.PRIMARY_COLOR or "#2563eb",
            "secondary_color": settings.SECONDARY_COLOR or "#64748b",
            "copyright": settings.COMPANY_COPYRIGHT or "© DealerPulse",
            "button_color": settings.BUTTON_COLOR or "#2563eb",
            "starting_year": settings.STARTING_YEAR or "2015",
        }

    @staticmethod
    def get_company_for_user(*, company_id, user):

        company = CompanyRepository.get_active_company(
            company_id=company_id,
        )

        if not user.belongs_to_company(company):
            raise ValidationError("You don't belong to this company")

        return company

    @staticmethod
    def switch_company(*, user, company):
        if not user.switch_company(company):
            raise ValidationError("Failed to switch company")
        return company

    @staticmethod
    def set_default_company(*, user, company):
        if not user.set_default_company(company):
            raise ValidationError("Failed to set default company")
        return company
