/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/authorization-code.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 { HlmInputImports } from '@spartan-ng/helm/input';
import { HlmLabelImports } from '@spartan-ng/helm/label';
import { HlmSwitchImports } from '@spartan-ng/helm/switch';
import { type OAuthAuthorizationCode } from '@src/src/app/aurora.types';

@Component({
  selector: 'au-authorization-code-form',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [
    ReactiveFormsModule,
    TranslocoDirective,
    ValidationMessagePipe,
    HlmFieldImports,
    HlmInputImports,
    HlmLabelImports,
    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-6">
          <label
            hlmLabel
            for="code"
          >
            {{ t('o-auth.authorization-code.Code') }}
            <span
              aria-hidden="true"
              class="text-muted-foreground/70 align-top text-xs"
            >
              *
            </span>
          </label>
          <input
            id="code"
            aria-required="true"
            hlmInput
            formControlName="code"
            maxlength="64"
          />
          <hlm-field-error>
            {{ signalForm.get('code')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
        <hlm-field class="col-span-12 md:col-span-12">
          <label
            hlmLabel
            for="redirectUri"
          >
            {{ t('o-auth.authorization-code.RedirectUri') }}
            <span
              aria-hidden="true"
              class="text-muted-foreground/70 align-top text-xs"
            >
              *
            </span>
          </label>
          <input
            id="redirectUri"
            aria-required="true"
            hlmInput
            formControlName="redirectUri"
            maxlength="2046"
          />
          <hlm-field-error>
            {{ signalForm.get('redirectUri')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>

        <hlm-field class="col-span-12 md:col-span-12">
          <label
            hlmLabel
            for="codeChallenge"
          >
            {{ t('o-auth.authorization-code.CodeChallenge') }}
          </label>
          <input
            id="codeChallenge"
            hlmInput
            formControlName="codeChallenge"
            maxlength="128"
          />
          <hlm-field-error>
            {{ signalForm.get('codeChallenge')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
        <hlm-field class="col-span-12 md:col-span-4">
          <label
            hlmLabel
            for="codeChallengeMethod"
          >
            {{ t('o-auth.authorization-code.CodeChallengeMethod') }}
          </label>
          <input
            id="codeChallengeMethod"
            hlmInput
            formControlName="codeChallengeMethod"
            maxlength="16"
          />
          <hlm-field-error>
            {{ signalForm.get('codeChallengeMethod')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
        <hlm-field class="col-span-12 md:col-span-3">
          <label
            hlmLabel
            for="expiresAt"
          >
            {{ t('o-auth.authorization-code.ExpiresAt') }}
            <span
              aria-hidden="true"
              class="text-muted-foreground/70 align-top text-xs"
            >
              *
            </span>
          </label>
          <hlm-date-picker
            aria-required="true"
            [buttonId]="'expiresAt'"
            formControlName="expiresAt"
          >
            {{ t('SelectDate') }}
          </hlm-date-picker>
          <hlm-field-error>
            {{ signalForm.get('expiresAt')?.errors | validationMessage }}
          </hlm-field-error>
        </hlm-field>
        <hlm-field class="col-span-12 pt-9 md:col-span-5">
          <label
            class="flex items-center"
            hlmLabel
            for="isUsed"
          >
            <hlm-switch
              class="mr-2"
              inputId="isUsed"
              formControlName="isUsed"
            />
            {{ t('o-auth.authorization-code.IsUsed') }}
          </label>
        </hlm-field>
      </form>
      <!-- #endregion AURORA:FORM-FIELDS-END -->
    </ng-container>
  `,
})
export class AuthorizationCodeFormComponent {
  // injections
  private readonly fb = inject(FormBuilder);

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

  // outputs
  readonly save = output<OAuthAuthorizationCode>();
  readonly cancel = output<void>();
  readonly signalForm: FormGroup = this.fb.group({
    id: [null],
    code: ['', [Validators.required, Validators.maxLength(64)]],
    clientId: [null],
    accountId: [null],
    redirectUri: ['', [Validators.required, Validators.maxLength(2046)]],
    scopes: [null],
    codeChallenge: ['', [Validators.maxLength(128)]],
    codeChallengeMethod: ['', [Validators.maxLength(16)]],
    expiresAt: [null, [Validators.required]],
    isUsed: [false],
  });
  constructor() {
    effect(() => {
      const init = this.initial();
      if (!init) return;
      this.signalForm.reset(init);
    });
  }

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