import { Component, OnInit } from '@angular/core';
import { DomainService } from '../rgnt-domain/service/domain.service';
// import { UserDomainService } from '../user-domain-details/service/user-domain.service';
import { lastValueFrom } from 'rxjs';
import { error } from 'jquery';
import { HttpStatusCode } from '@angular/common/http';
import { Router } from '@angular/router';
import { query } from '@angular/animations';
import { OrganisationDetailsService } from '../organisation-details/service/organisation-details.service';

@Component({
    selector: 'app-application-submission',
    templateUrl: './application-submission.component.html',
    styleUrls: ['./application-submission.component.css'],
    standalone: false
})
export class ApplicationSubmissionComponent implements OnInit {

  userMailId: string = localStorage.getItem('email');
  currentOnboardingOrganization:any=null;
  // currentOnboardingDomain: any = null;

  // async ngOnInit() {
  //   await this.getDomainApplicationDetailsOfOnboardingDomain(this.userMailId, true);
  // }

//  async ngOnInit() {
//   const orgId = Number(localStorage.getItem('organisationId'));
//   if (orgId) {
//     await this.getOrganisationDetailsByOrganisationId(orgId);
//   } else {
//     console.warn("No organisationId found in localStorage");
//   }
// }
// async ngOnInit() {
//   const orgId = Number(localStorage.getItem('organisationDetailsId')); // ✅ FIXED

//   if (orgId) {
//     await this.getOrganisationDetailsByOrganisationId(orgId);
//   } else {
//     console.warn("No organisationId found in localStorage");
//   }
// }

// async ngOnInit() {

//   // 🔥 ADD HERE
//   console.log("ORG ID FROM STORAGE:", localStorage.getItem('organisationDetailsId'));

//   const orgId = Number(localStorage.getItem('organisationDetailsId'));

//   if (orgId) {
//     await this.getOrganisationDetailsByOrganisationId(orgId);
//   } else {
//     console.warn("No organisationId found in localStorage");
//   }
// }
async ngOnInit() {

  const orgIdFromDetails = localStorage.getItem('organisationDetailsId');

  console.log("ORG ID FROM STORAGE:", orgIdFromDetails);

  if (orgIdFromDetails && Number(orgIdFromDetails) > 0) {
    await this.getOrganisationDetailsByOrganisationId(Number(orgIdFromDetails));
  } else {
    console.warn("No organisationDetailsId found in localStorage");
  }
}

   constructor(private orgService: OrganisationDetailsService,
    private router: Router,
    
  ) { 
  }

async getOrganisationDetailsByOrganisationId(orgId: number) {
  console.log("Fetching organization by ID:", orgId);

  await lastValueFrom(this.orgService.getOrganisationDetailsByOrganisationId(orgId)).then(
    (response) => {
      this.currentOnboardingOrganization = response.body;
      console.log("Fetched organization:", this.currentOnboardingOrganization);
    },
    (error) => {
      if (error.status === HttpStatusCode.Unauthorized) {
        this.navigateToSessionTimeout();
      }
    }
  );
}

  navigateToSessionTimeout() {
    this.router.navigate(['/session-timeout']);
  }
  
  trackApplication() {
    if (this.currentOnboardingOrganization?.organisationDetailsId) {
      this.router.navigate(['/rgnt-entity-app-details'], { 
        queryParams: { orgId: this.currentOnboardingOrganization.organisationDetailsId }
      });
    } else {
    }
  }
  

}
