import {
  Component,
  computed,
  effect,
  inject,
  input,
  linkedSignal,
} from '@angular/core';
import {
  BranchResource,
  getOrderTypeFeature,
  ShoppingCartItem,
} from '@isa/checkout/data-access';
import { SelectedCustomerShippingAddressResource } from '@isa/crm/data-access';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { OrderDestinationComponent } from '@isa/shared/delivery';
import { logger } from '@isa/core/logging';

export type DestinationInfo = {
  features: ShoppingCartItem['features'];
  availability: ShoppingCartItem['availability'];
  destination: ShoppingCartItem['destination'];
};

@Component({
  selector: 'checkout-destination-info',
  templateUrl: './destination-info.component.html',
  styleUrls: ['./destination-info.component.css'],
  imports: [OrderDestinationComponent],
  providers: [BranchResource],
})
export class DestinationInfoComponent {
  #logger = logger({ component: 'DestinationInfoComponent' });
  #branchResource = inject(BranchResource);
  #shippingAddressResource = inject(SelectedCustomerShippingAddressResource);

  underline = input<boolean, unknown>(false, {
    transform: coerceBooleanProperty,
  });

  shoppingCartItem = input.required<DestinationInfo>();

  orderType = computed(() => {
    const features = this.shoppingCartItem().features;
    const orderType = getOrderTypeFeature(features);
    this.#logger.debug('Computing order type', () => ({ orderType, features }));
    return orderType;
  });

  branchContainer = computed(
    () => this.shoppingCartItem().destination?.data?.targetBranch,
  );

  branch = linkedSignal(
    () => this.branchContainer()?.data ?? this.#branchResource.resource.value(),
  );

  branchChange = effect(() => {
    const branchContainer = this.branchContainer();
    if (branchContainer?.data && branchContainer?.id) {
      this.#branchResource.params({ branchId: branchContainer.id });
    } else {
      this.#branchResource.params({ branchId: null, branchNumber: null });
    }
  });

  mappedBranch = computed(() => {
    const branch = this.branch();
    return branch
      ? {
          name: branch.name,
          address: branch.address,
        }
      : undefined;
  });

  mappedShippingAddress = computed(() => {
    const address = this.#shippingAddressResource.resource.value();
    return address
      ? {
          firstName: address.firstName,
          lastName: address.lastName,
          address: address.address,
        }
      : undefined;
  });

  estimatedDelivery = computed(() => {
    const availability = this.shoppingCartItem().availability;
    const estimatedDelivery = availability?.estimatedDelivery;
    const estimatedShippingDate = availability?.estimatedShippingDate;

    if (estimatedDelivery?.start && estimatedDelivery?.stop) {
      return { start: estimatedDelivery.start, stop: estimatedDelivery.stop };
    }

    if (estimatedShippingDate) {
      return { start: estimatedShippingDate, stop: null };
    }

    return null;
  });
}
