from __future__ import annotations

from sqlalchemy import select
from sqlalchemy.orm import Session

from app.models.customer import Customer
from app.models.device import Device
from app.models.document import Document
from app.models.equipment import Equipment
from app.models.contact import Contact
from app.models.location import Location
from app.models.user import User
from app.models.validation import Validation
from app.repositories.base import Repository


class UserRepository(Repository[User]):
    model = User

    def by_email(self, email: str) -> User | None:
        return self.session.scalar(select(User).where(User.email == email.lower()))


class CustomerRepository(Repository[Customer]):
    model = Customer
    search_columns = ("name", "city", "email", "phone")


class LocationRepository(Repository[Location]):
    model = Location
    search_columns = ("name", "city", "room")


class ContactRepository(Repository[Contact]):
    model = Contact
    search_columns = ("full_name", "function", "email", "phone")


class DeviceRepository(Repository[Device]):
    model = Device
    search_columns = ("manufacturer", "model", "serial_number", "device_type")


class EquipmentRepository(Repository[Equipment]):
    model = Equipment
    search_columns = ("manufacturer", "model", "serial_number")


class ValidationRepository(Repository[Validation]):
    model = Validation


class DocumentRepository(Repository[Document]):
    model = Document


def repositories(session: Session) -> dict[str, Repository]:
    return {
        "users": UserRepository(session),
        "customers": CustomerRepository(session),
        "locations": LocationRepository(session),
        "contacts": ContactRepository(session),
        "devices": DeviceRepository(session),
        "equipment": EquipmentRepository(session),
        "validations": ValidationRepository(session),
        "documents": DocumentRepository(session),
    }
