/*
   * To verify that NMS is not able to change the location of
   * beneficiary using MCTS ID when location information is wrong.
   *
   * https://applab.atlassian.net/browse/NMS-231
   */
  @Test
  public void verifyFT325() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String childId = "0123456789";

    MctsChild child = new MctsChild(childId);
    child.setState(stateDataService.findByCode(21L));
    child.setDistrict(districtService.findByStateAndCode(child.getState(), 2L));
    makeMctsSubscription(child, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, msisdn);

    // district provided in request doesn't exist in nms-db
    Reader reader = createUpdateReaderWithHeaders("1," + childId + ",,,,21,8,0026,453,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertNotNull(subscriber);
    assertNotEquals(subscriber.getChild().getDistrict().getCode(), new Long(7));

    List<SubscriptionError> susbErrors = subscriptionErrorDataService.findByBeneficiaryId(childId);
    SubscriptionError susbError = susbErrors.iterator().next();
    assertNotNull(susbError);
    assertEquals(SubscriptionRejectionReason.INVALID_LOCATION, susbError.getRejectionReason());
  }
  @Test
  public void testUpdateDOBAndCompleteActiveSubscription() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String childId = "0123456789";
    DateTime originalDOB = DateTime.now().minusDays(100);
    DateTime updatedDOB = originalDOB.minusDays(400);

    MctsChild child = new MctsChild(childId);
    child.setState(stateDataService.findByCode(21L));
    child.setDistrict(districtService.findByStateAndCode(child.getState(), 3L));
    makeMctsSubscription(child, originalDOB, SubscriptionPackType.CHILD, msisdn);

    // verify that the subscription is active
    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    Subscription subscription = subscriber.getAllSubscriptions().iterator().next();
    assertEquals(SubscriptionStatus.ACTIVE, subscription.getStatus());

    // now, via CSV update, change the DOB to a past subscription date; subscription should be
    // marked completed
    Reader reader =
        createUpdateReaderWithHeaders(
            "1," + childId + ",," + getDateString(updatedDOB) + ",,,,,,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber updatedSubscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscriber.getDateOfBirth()));
    Subscription updatedSubscription = updatedSubscriber.getAllSubscriptions().iterator().next();
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.COMPLETED, updatedSubscription.getStatus());
  }
  @Test
  public void testUpdateLMP() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String motherId = "0123456789";
    DateTime originalLMP = DateTime.now().minusDays(100);
    DateTime updatedLMP = originalLMP.minusDays(200);

    subscriptionHelper.mksub(
        SubscriptionOrigin.MCTS_IMPORT, originalLMP, SubscriptionPackType.PREGNANCY, msisdn);
    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    subscriber.setLastMenstrualPeriod(originalLMP);
    MctsMother mother = new MctsMother(motherId);
    mother.setState(stateDataService.findByCode(21L));
    mother.setDistrict(districtService.findByStateAndCode(mother.getState(), 3L));
    subscriber.setMother(mother);
    subscriberDataService.update(subscriber);

    Reader reader =
        createUpdateReaderWithHeaders(
            "1," + motherId + ",,," + getDateString(updatedLMP) + ",,,,,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    Subscriber updatedSubscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertEquals(
        getDateString(updatedLMP), getDateString(updatedSubscriber.getLastMenstrualPeriod()));
    Subscription updatedSubscription =
        updatedSubscriber.getActiveAndPendingSubscriptions().iterator().next();
    assertEquals(
        getDateString(updatedLMP.plusDays(90)), getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.ACTIVE, updatedSubscription.getStatus());
  }
  @Test
  public void testUpdateDOB() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String childId = "0123456789";
    DateTime originalDOB = DateTime.now();
    DateTime updatedDOB = originalDOB.minusDays(100);

    subscriptionHelper.mksub(
        SubscriptionOrigin.MCTS_IMPORT, originalDOB, SubscriptionPackType.CHILD, msisdn);
    Subscriber subscriber = subscriberDataService.findByCallingNumber(msisdn);
    subscriber.setDateOfBirth(originalDOB);
    MctsChild child = new MctsChild(childId);
    child.setState(stateDataService.findByCode(21L));
    child.setDistrict(districtService.findByStateAndCode(child.getState(), 3L));
    subscriber.setChild(child);
    subscriberDataService.update(subscriber);

    // this updates the db with the new data (DOB)
    Reader reader =
        createUpdateReaderWithHeaders(
            "1," + childId + ",," + getDateString(updatedDOB) + ",,,,,,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    // This query should return the updated subscriber information (but it doesn't...causing the
    // assert to fail)
    Subscriber updatedSubscriber = subscriberDataService.findByCallingNumber(msisdn);
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscriber.getDateOfBirth()));
    Subscription updatedSubscription =
        updatedSubscriber.getActiveAndPendingSubscriptions().iterator().next();
    assertEquals(getDateString(updatedDOB), getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.ACTIVE, updatedSubscription.getStatus());
  }
  @Test
  public void testUpdateLocation() throws Exception {
    createLocationData();

    Long msisdn = subscriptionHelper.makeNumber();
    String childId = "0123456789";

    MctsChild child = new MctsChild(childId);
    child.setState(stateDataService.findByCode(21L));
    child.setDistrict(districtService.findByStateAndCode(child.getState(), 2L));
    makeMctsSubscription(child, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, msisdn);

    Reader reader = createUpdateReaderWithHeaders("1," + childId + ",,,,21,3,0026,453,,,,,,");
    mctsBeneficiaryUpdateService.updateBeneficiaryData(reader);

    MctsChild updatedChild = mctsChildDataService.findByBeneficiaryId(childId);
    assertEquals(21L, (long) updatedChild.getState().getCode());
    assertEquals(3L, (long) updatedChild.getDistrict().getCode());
    assertEquals("0026", updatedChild.getTaluka().getCode());
    assertEquals(453L, (long) updatedChild.getHealthBlock().getCode());
  }
  @Test
  public void testUpdateBeneficiariesFromFile() throws Exception {
    createLocationData();

    // ----Create 4 beneficiaries:----

    String child1id = "1234567890";
    MctsChild child1 = new MctsChild(child1id);
    child1.setState(stateDataService.findByCode(21L));
    child1.setDistrict(districtService.findByStateAndCode(child1.getState(), 3L));
    Long child1msisdn = subscriptionHelper.makeNumber();
    makeMctsSubscription(
        child1, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, child1msisdn);

    String mother2id = "1234567899";
    MctsMother mother2 = new MctsMother(mother2id);
    mother2.setState(stateDataService.findByCode(21L));
    mother2.setDistrict(districtService.findByStateAndCode(mother2.getState(), 3L));
    Long mother2msisdn = subscriptionHelper.makeNumber();
    makeMctsSubscription(
        mother2, DateTime.now().minusDays(100), SubscriptionPackType.PREGNANCY, mother2msisdn);

    String mother3id = "9876543210";
    MctsMother mother3 = new MctsMother(mother3id);
    mother3.setState(stateDataService.findByCode(21L));
    mother3.setDistrict(districtService.findByStateAndCode(mother3.getState(), 3L));
    makeMctsSubscription(
        mother3,
        DateTime.now().minusDays(100),
        SubscriptionPackType.PREGNANCY,
        subscriptionHelper.makeNumber());

    Long child4msisdn = subscriptionHelper.makeNumber();
    MctsChild child4 = new MctsChild("9876543211");
    child4.setState(stateDataService.findByCode(21L));
    child4.setDistrict(districtService.findByStateAndCode(child4.getState(), 3L));
    makeMctsSubscription(
        child4, DateTime.now().minusDays(100), SubscriptionPackType.CHILD, child4msisdn);

    // ----Update all 4 via CSV:----

    mctsBeneficiaryUpdateService.updateBeneficiaryData(read("csv/mcts_beneficiary_update.csv"));

    // ----Validate updates to each:----

    // MSISDN update:
    Subscriber oldSubscriber1 = subscriberDataService.findByCallingNumber(child1msisdn);
    assertNull(oldSubscriber1.getChild());
    assertEquals(0, oldSubscriber1.getAllSubscriptions().size());

    Subscriber subscriber1 = subscriberDataService.findByCallingNumber(9439986187L);
    assertNotNull(subscriber1);
    assertEquals(child1id, subscriber1.getChild().getBeneficiaryId());

    // MSISDN update:
    Subscriber oldSubscriber2 = subscriberDataService.findByCallingNumber(mother2msisdn);
    assertNull(oldSubscriber2.getMother());
    assertEquals(0, oldSubscriber2.getAllSubscriptions().size());

    Subscriber subscriber2 = subscriberDataService.findByCallingNumber(9439986188L);
    assertNotNull(subscriber2);
    assertEquals(mother2id, subscriber2.getMother().getBeneficiaryId());

    // Location update:
    MctsMother updatedMother3 = mctsMotherDataService.findByBeneficiaryId(mother3id);
    assertEquals(21L, (long) updatedMother3.getState().getCode());
    assertEquals(3L, (long) updatedMother3.getDistrict().getCode());
    assertEquals("0026", updatedMother3.getTaluka().getCode());
    assertEquals(453L, (long) updatedMother3.getHealthBlock().getCode());

    // DOB update:
    String updatedDOB = "01-07-2015";
    Subscriber subscriber4 = subscriberDataService.findByCallingNumber(child4msisdn);
    assertEquals(updatedDOB, getDateString(subscriber4.getDateOfBirth()));
    Subscription updatedSubscription =
        subscriber4.getActiveAndPendingSubscriptions().iterator().next();
    assertEquals(updatedDOB, getDateString(updatedSubscription.getStartDate()));
    assertEquals(SubscriptionStatus.ACTIVE, updatedSubscription.getStatus());
  }