package com.eddsteel.posts.leastpower
package endpoints

import model._
import services._
import webapp._

import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.{when, verify, RETURNS_SMART_NULLS}

import scala.concurrent.{Future, ExecutionContext}
import java.time.{ZoneOffset, LocalDate, ZonedDateTime, ZoneId}

class EndpointTestsV2 extends FlatSpec with Matchers with MockitoSugar with ScalaFutures {

  import ExecutionContext.Implicits.global

  val mockGiftService = mock[GiftService](RETURNS_SMART_NULLS)

  "maybeSendGift" should "order a gift for a user on their birthday" in {
    DateTimeUtils.setDateTime(ZonedDateTime.of(2015, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))

    val fakeUser =
      User(id = 1, birthday = LocalDate.of(1980, 1, 1), favoriteThing = "booze",
           timezone = ZoneOffset.UTC, pets = Set.empty, whichFriendAreYou = Friends.Phoebe)

    when(mockGiftService.order(1L, "booze")).thenReturn(
      Future.successful(("booze ordered for user 1", Some(1L))))

    val response = EndpointsV2.maybeSendGift(fakeUser, mockGiftService)
    response.futureValue.shouldEqual(("booze ordered for user 1", Some(1L)))
    verify(mockGiftService).order(1L, "booze") // check the gift actually was ordered through the service
  }
}