import { BreadcrumbService } from '@/core/services';
import { AppCardComponent, KeyValueComponent } from '@/shared/components';
import { Component, computed, effect, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { Button } from 'primeng/button';
import { COOKIE_KEYS } from 'src/assets/constants';
import { ConsumerPosFormComponent } from '../components/form.component';
import { ConsumerPosStore } from '../store/pos.store';

@Component({
  selector: 'consumer-pos-page',
  templateUrl: './single.component.html',
  imports: [AppCardComponent, KeyValueComponent, Button, ConsumerPosFormComponent],
})
export class ConsumerPosPageComponent {
  private readonly store = inject(ConsumerPosStore);
  private readonly route = inject(ActivatedRoute);
  private readonly breadcrumbService = inject(BreadcrumbService);
  private readonly cookieService = inject(CookieService);

  readonly posId = signal<string>(this.route.snapshot.paramMap.get('posId')!);
  editMode = signal<boolean>(false);

  readonly loading = computed(() => this.store.loading());
  readonly pos = computed(() => this.store.entity());
  readonly posBreadcrumb = computed(() => this.store.breadcrumbItems());

  constructor() {
    effect(() => {
      if (this.pos()?.id) {
        this.setBreadcrumb();
      }
    });
  }

  getData() {
    this.store.getData(this.posId());
  }

  setBreadcrumb() {
    this.breadcrumbService.setItems(this.posBreadcrumb());
  }

  toPosLanding() {
    this.cookieService.set(COOKIE_KEYS.POS_ID, '', new Date());
    this.cookieService.set(COOKIE_KEYS.POS_ID, this.posId(), {
      sameSite: 'Lax', // or 'Strict' for same-site requests only
      secure: false,
      path: '/',
      domain: 'localhost',
    });
    window.open('/pos', '_blank');
  }
}
