import { Component, OnInit } from '@angular/core';
import { BaseService } from 'src/app/shared/service/base.service';
import { StorageService } from 'src/app/shared/service/storage.service';
import { Plan } from '../products/pricing.model';

@Component({
  selector: 'app-myplans',
  templateUrl: './myplans.component.html',
  styleUrls: ['./myplans.component.scss']
})
export class MyplansComponent implements OnInit {
  breadCrumbItems: Array<{}>;
  myproduct: Plan[] = [];
  products: Plan[] = [];
  id: number = 1;
  fieldsToHide: string[] = ['hard_limit', 'stripe_product_id', 'price_id', 'is_subscribed', 'is_free', 'is_custom', 'is_visible', 'is_quota_exceeded'];

  constructor(private baseService: BaseService, private storage: StorageService) { }

  ngOnInit() {
    if ("Tenant Admin" == this.storage.getItem('role')) {
      this.baseService.getData('subscriptions/getPlans').subscribe(
        (data: any) => {
          if (data && data.data) {
            this.products = data.data;
            console.log(this.products);
            this.activePlan()
          } else {
            console.error('Invalid plan data:', data);
          }
        },
        (err) => {
          console.log(err);
        }
      );
    }
  }

  activePlan(){

    const userid = this.storage.getItem('user');
    this.baseService.getData(`tenants/getbyuserid/${userid}`).subscribe(
      (data: any) => {
        const tenantuuid = data.data[0].tenantUUID;
        this.baseService.getData(`subscriptions/getSubscriptionbyTenantid/${tenantuuid}`).subscribe(
          (data: any) => {
            console.log('Subscription data:', data.data); // Log the data to understand its structure
            if (data && data.data) {
              const subscription = data.data; // Single subscription object
              const product = this.products.find(p => p.id === subscription.planId);
              if (product) {
                this.myproduct = [product];
              }
              console.log(this.myproduct);
            } else {
              console.error('Invalid subscription data:', data);
            }
          },
          (err) => {
            console.log(err);
          }
      );
      },
      error => {
        console.error('Request failed', error);
      }
    );

    
  }

  shouldHideField(fieldName: string): boolean {
    return this.fieldsToHide.includes(fieldName.toLowerCase());
  }

  getAttributeValue(attributes: any[], attrName: string): string {
    const attr = attributes.find(attribute => attribute.attrName === attrName);
    return attr ? attr.attrValue : '';
  }

  toggleExpand(event: Event) {
    const cardBody = (event.target as HTMLElement).closest('.card-body');
    if (cardBody) {
      cardBody.classList.toggle('expanded');
      const readMoreLink = cardBody.querySelector('.read-more') as HTMLElement;
      if (cardBody.classList.contains('expanded')) {
        readMoreLink.innerText = 'Read less';
      } else {
        readMoreLink.innerText = 'Read more';
      }
    }
  }
}
