from datetime import datetime

from core.config import get_db
from core.security.authentication import hash_password
from core.security.exceptions import GenericError
from models import User
from models.staff import Staff
from schema.staff import StaffResponse


class StaffService:
    def create_new_staff(self, request):

        with get_db() as db:
            try:
                payload = request.model_dump(exclude=['password'])
                payload['hashed_password'] =  hash_password(request.password)
                new_staff = Staff(**payload)
                db.add(new_staff)
                db.commit()
            except Exception as e:
                print(e)
                db.rollback()



    def fetch_all_staffs(self):
        with get_db() as db:
            records = db.query(Staff).where(Staff.deleted_at.is_(None)).all()
            return [StaffResponse.model_validate(record).model_dump() for record in records]

    def fetch_staff_detail(self, staff_id: int):
        with get_db() as db:
            record = db.query(Staff).where(
                Staff.id == staff_id,
                Staff.deleted_at.is_(None)
            ).first()

            if not record:
                raise GenericError(status_code=404, exc="Staff not found")
            return StaffResponse.model_validate(record).model_dump()

    def get_staff_profile_detail(self, staff_id: int):
        with get_db() as db:
            record = db.query(Staff).where(
                Staff.id == staff_id,
                Staff.deleted_at.is_(None)
            ).first()

            if not record:
                raise GenericError(status_code=404, exc="Staff not found")
            return StaffResponse.model_validate(record).model_dump()

    def update_staff(self, staff_id, request):

        payload = request.model_dump(exclude=['id'])
        with get_db() as db:
            staff = db.query(Staff).filter(Staff.id == staff_id).first()
            if not staff:
                raise GenericError(status_code=422, exc="Could not update teacher")
            staff.first_name = request.first_name
            staff.last_name = request.last_name
            staff.email = request.email
            staff.mobile_number = request.mobile_number
            db.commit()


    def update_staff_profile(self, staff_id, request):

        payload = request.model_dump(exclude=['id'])
        with get_db() as db:
            staff = db.query(Staff).filter(Staff.id == staff_id).first()
            if not staff:
                raise GenericError(status_code=422, exc="Could not update teacher")
            staff.first_name = request.first_name
            staff.last_name = request.last_name
            staff.email = request.email
            staff.mobile_number = request.mobile_number
            staff.dbs_status= request.dbs_status
            db.commit()


    def delete_staff(self, staff_id):
        with get_db() as db:
            staff = db.query(Staff).filter(Staff.id == staff_id).first()
            if not staff:
                raise GenericError(status_code=404, exc="Staff not found")

        # Soft delete the staff record
        staff.deleted_at = datetime.now()
        db.commit()
        return {"message": "Staff deleted successfully"}