import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  signal,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CheckboxComponent } from '@isa/ui/input-controls';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { isaActionChevronDown, isaActionChevronUp } from '@isa/icons';
import { CheckboxFilterInputOption, FilterService } from '../../core';
import { checkboxSelectedHelper } from '../../helpers';

/**
 * Component that renders an individual checkbox control within a hierarchical filter structure.
 *
 * This component handles:
 * - Three-state checkbox display (checked, indeterminate, unchecked)
 * - Parent-child relationships with expand/collapse functionality
 * - Automatic state calculation based on children selection
 * - Integration with the FilterService for state management
 *
 * @example
 * ```html
 * <filter-checkbox-input-control
 *   [option]="checkboxOption">
 * </filter-checkbox-input-control>
 * ```
 */
@Component({
  selector: 'filter-checkbox-input-control',
  templateUrl: './checkbox-input-control.component.html',
  styleUrls: ['./checkbox-input-control.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [FormsModule, CheckboxComponent, NgIcon],
  providers: [provideIcons({ isaActionChevronDown, isaActionChevronUp })],
})
export class CheckboxInputControlComponent {
  /** Filter service for managing checkbox state */
  readonly filterService = inject(FilterService);

  /** The checkbox option configuration */
  option = input.required<CheckboxFilterInputOption>();

  /**
   * Computed signal that determines the current state of the checkbox.
   * Uses the checkboxSelectedHelper to calculate state based on parent-child relationships.
   */
  checkboxState = computed(() => {
    return checkboxSelectedHelper(this.filterService.inputs(), this.option());
  });

  /**
   * Whether the checkbox is fully selected (checked state).
   * Returns true only when the checkbox is in 'checked' state, not 'indeterminate'.
   */
  selected = computed<boolean>(() => {
    const state = this.checkboxState();
    return state === 'checked';
  });

  /**
   * Whether the checkbox is in an indeterminate state.
   * This occurs when a parent checkbox has some but not all children selected.
   */
  indeterminate = computed<boolean>(() => {
    return this.checkboxState() === 'indeterminate';
  });

  /**
   * Updates the selection state of this checkbox option.
   *
   * @param value - Whether to select (true) or deselect (false) the option
   */
  setSelected(value: boolean) {
    const option = this.option();
    this.filterService.setInputCheckboxOptionSelected(option, value);
  }

  /** Signal controlling whether child options are expanded/visible */
  expanded = signal(false);

  /**
   * Determines if this checkbox option has children.
   * Parent options can be expanded to show their children.
   */
  isParent = computed(() => {
    const values = this.option().values;

    return Array.isArray(values) && values.length > 0;
  });

  /**
   * Computed icon name for the expand/collapse chevron.
   * Shows up arrow when expanded, down arrow when collapsed.
   */
  expandedIconName = computed(() => {
    return this.expanded() ? 'isaActionChevronUp' : 'isaActionChevronDown';
  });
}
