// import { CatalogRoleTagComponent } from '@/shared/catalog/roles';
import { AbstractList } from '@/shared/abstractClasses/abstract-list';
import {
  IColumn,
  PageDataListComponent,
} from '@/shared/components/pageDataList/page-data-list.component';
import { posListConfig } from '@/shared/constants/list-configs';
import { Component, inject, Input, TemplateRef, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { Button } from 'primeng/button';
import { COOKIE_KEYS } from 'src/assets/constants';
import { ConsumerPosesNamedRoutes } from '../constants';
import { IPosResponse } from '../models';
import { ConsumerPosesService } from '../services/main.service';
import { ConsumerPosFormComponent } from './form.component';

@Component({
  selector: 'consumer-pos-list',
  templateUrl: './list.component.html',
  imports: [PageDataListComponent, ConsumerPosFormComponent, Button],
})
export class ConsumerPosListComponent extends AbstractList<IPosResponse> {
  @Input() fullHeight?: boolean;
  @Input() header: IColumn[] = posListConfig.columns;

  private readonly service = inject(ConsumerPosesService);
  private readonly cookieService = inject(CookieService);
  private readonly router = inject(Router);

  @ViewChild('toPosLandingAction', { static: true }) toPosLandingAction!: TemplateRef<any>;

  override setColumns(): void {
    this.columns = [
      ...this.header.filter((header) =>
        ['name', 'account', 'pos_type', 'status'].includes(header.field as string),
      ),
      {
        field: 'complex.business_activity',
        header: 'عنوان کسب و کار',
        type: 'nested',
        nestedOption: { path: 'complex.business_activity.name' },
      },
      {
        field: 'complex',
        header: 'عنوان شعبه',
        type: 'nested',
        nestedOption: { path: 'complex.name' },
      },
      {
        field: 'customAction',
        header: '',
        width: '128px',
        minWidth: '128px',
        customDataModel: this.toPosLandingAction,
      },
    ];
  }

  override getDataRequest() {
    return this.service.getAll();
  }

  toSinglePage(item: IPosResponse) {
    this.router.navigateByUrl(ConsumerPosesNamedRoutes.pos.meta.pagePath!(item.id));
  }

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