/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/refresh-token.aurora.yaml
 */
import { ChangeDetectionStrategy, Component, effect, inject, input, output } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { ValidationMessagePipe } from '@aurora';
import { provideTranslocoScope, TranslocoDirective } from '@jsverse/transloco';
import { HlmDatePickerImports } from '@spartan-ng/helm/date-picker';
import { HlmFieldImports } from '@spartan-ng/helm/field';
import { HlmLabelImports } from '@spartan-ng/helm/label';
import { HlmSwitchImports } from '@spartan-ng/helm/switch';
import { HlmTextareaImports } from '@spartan-ng/helm/textarea';
import { type OAuthRefreshToken } from '@src/src/app/aurora.types';

@Component({
  selector: 'au-refresh-token-form',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    ReactiveFormsModule,
    TranslocoDirective,
    ValidationMessagePipe,
    HlmFieldImports,
    HlmLabelImports,
    HlmTextareaImports,
    HlmSwitchImports,
    HlmDatePickerImports,
  ],
  providers: [provideTranslocoScope('o-auth')],
  template: `
    <ng-container *transloco="let t; scope: 'o-auth'">
      <!-- #region AURORA:FORM-FIELDS-START -->
      <form
        class="grid grid-cols-1 gap-x-4 gap-y-5 md:grid-cols-12"
        [formGroup]="signalForm"
        (ngSubmit)="submit()"
      >
        <hlm-field class="col-span-12 md:col-span-12">
          <label
            hlmLabel
            for="token"
          >
            {{ t('o-auth.refresh-token.Token') }}
            <span
              aria-hidden="true"
              class="text-muted-foreground/70 align-top text-xs"
            >
              *
            </span>
          </label>
          <textarea
            id="token"
            aria-required="true"
            hlmTextarea
            formControlName="token"
          ></textarea>
          <hlm-field-error>
            {{ signalForm.get('token')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
        <hlm-field class="col-span-12 pt-5 md:col-span-3">
          <label
            class="flex items-center"
            hlmLabel
            for="isRevoked"
          >
            <hlm-switch
              class="mr-2"
              inputId="isRevoked"
              formControlName="isRevoked"
            />
            {{ t('o-auth.refresh-token.IsRevoked') }}
          </label>
        </hlm-field>

        <hlm-field class="col-span-12 md:col-span-9">
          <label
            hlmLabel
            for="expiresAt"
          >
            {{ t('o-auth.refresh-token.ExpiresAt') }}
          </label>
          <hlm-date-picker
            [buttonId]="'expiresAt'"
            formControlName="expiresAt"
          >
            {{ t('SelectDate') }}
          </hlm-date-picker>
          <hlm-field-error>
            {{ signalForm.get('expiresAt')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
      </form>
      <!-- #endregion AURORA:FORM-FIELDS-END -->
    </ng-container>
  `,
})
export class RefreshTokenFormComponent {
  // injections
  private readonly fb = inject(FormBuilder);

  // inputs
  readonly initial = input<OAuthRefreshToken | null>(null);
  readonly mode = input.required<'new' | 'edit'>();

  // outputs
  readonly save = output<OAuthRefreshToken>();
  readonly cancel = output<void>();
  readonly signalForm: FormGroup = this.fb.group({
    id: [null],
    accessTokenId: [null],
    token: ['', [Validators.required]],
    isRevoked: [false],
    expiresAt: [null],
  });

  constructor() {
    effect(() => {
      const init = this.initial();
      if (init) this.signalForm.reset(init);
    });
  }

  submit(): void {
    if (this.signalForm.invalid) {
      this.signalForm.markAllAsTouched();
      return;
    }
    this.save.emit(this.signalForm.value as OAuthRefreshToken);
  }
}
