use crate::extractors::family_extractor::FamilyInPath;
use crate::models::{
    Accommodation, AccommodationReservation, AccommodationReservationID, FamilyID, Membership,
};
use crate::services::{accommodations_list_service, accommodations_reservations_service};
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use serde::Deserialize;
use std::fmt::Debug;
use std::ops::Deref;

#[derive(thiserror::Error, Debug)]
enum AccommodationReservationExtractorErr {
    #[error("Accommodation reservation {0:?} does not belong to family {1:?}!")]
    AccommodationNotInFamily(AccommodationReservationID, FamilyID),
}

#[derive(Debug)]
pub struct FamilyAndAccommodationReservationInPath(
    Membership,
    Accommodation,
    AccommodationReservation,
);

impl FamilyAndAccommodationReservationInPath {
    async fn load_accommodation_reservation_from_path(
        family: FamilyInPath,
        reservation_id: AccommodationReservationID,
    ) -> anyhow::Result<Self> {
        let reservation = accommodations_reservations_service::get_by_id(reservation_id).await?;
        let accommodation =
            accommodations_list_service::get_by_id(reservation.accommodation_id()).await?;

        if accommodation.family_id() != family.family_id()
            || reservation.family_id() != family.family_id()
        {
            return Err(
                AccommodationReservationExtractorErr::AccommodationNotInFamily(
                    reservation.id(),
                    family.family_id(),
                )
                .into(),
            );
        }

        Ok(Self(family.into(), accommodation, reservation))
    }
}

impl Deref for FamilyAndAccommodationReservationInPath {
    type Target = AccommodationReservation;

    fn deref(&self) -> &Self::Target {
        &self.2
    }
}

impl FamilyAndAccommodationReservationInPath {
    pub fn membership(&self) -> &Membership {
        &self.0
    }

    pub fn as_accommodation(&self) -> &Accommodation {
        &self.1
    }

    pub fn to_accommodation(self) -> Accommodation {
        self.1
    }

    pub fn to_reservation(self) -> AccommodationReservation {
        self.2
    }
}

#[derive(Deserialize)]
struct ReservationIDInPath {
    reservation_id: AccommodationReservationID,
}

impl FromRequest for FamilyAndAccommodationReservationInPath {
    type Error = actix_web::Error;
    type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;

    fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
        let req = req.clone();
        Box::pin(async move {
            let family = FamilyInPath::extract(&req).await?;

            let reservation_id =
                actix_web::web::Path::<ReservationIDInPath>::from_request(&req, &mut Payload::None)
                    .await?
                    .reservation_id;

            Self::load_accommodation_reservation_from_path(family, reservation_id)
                .await
                .map_err(|e| {
                    log::error!("Failed to extract accommodation ID from URL! {e}");
                    actix_web::error::ErrorNotFound("Could not fetch accommodation information!")
                })
        })
    }
}
