import { css, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators.js";

@customElement("secondary-button")
export class SecondaryButton extends LitElement {
  @property({ type: String }) label = "Button";
  @property({ type: String }) icon = "";
  @property({ type: String }) iconPosition: "left" | "right" = "right";
  @property({ type: Boolean }) disabled = false;
  @property({ attribute: false }) onClick?: () => void;

  static styles = css`
    :host {
      display: inline-block;
    }

    button {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 0.5rem;
      width: 100%;
      padding: 0.625rem 1.25rem;
      background-color: transparent;
      color: var(--color-text, #111);
      border: 1.5px solid var(--color-border-strong, #aaa);
      border-radius: 0.375rem;
      font-family: "Inter", system-ui, sans-serif;
      font-size: var(--font-size-base, 0.9375rem);
      font-weight: var(--font-weight-medium, 500);
      cursor: pointer;
      transition: background-color 0.2s ease, border-color 0.25s ease, color 0.25s ease;
    }

    button:hover {
      background-color: var(--color-border, #ddd);
    }

    button:active {
      background-color: var(--color-border, #ddd);
      filter: brightness(0.95);
    }

    button:focus-visible {
      outline: 2px solid var(--color-accent, #2b6cb0);
      outline-offset: 3px;
    }

    button:disabled {
      opacity: 0.45;
      cursor: not-allowed;
      filter: none;
    }

    button:disabled:hover,
    button:disabled:active {
      background-color: transparent;
    }

    hero-icon {
      width: 1.1rem;
      height: 1.1rem;
      flex-shrink: 0;
    }
  `;

  private _handleClick() {
    this.onClick?.();
  }

  render() {
    const iconEl = this.icon
      ? html`<hero-icon name=${this.icon}></hero-icon>`
      : null;

    return html`
      <button @click=${this._handleClick} ?disabled=${this.disabled}>
        ${this.iconPosition === "left" ? iconEl : null}
        ${this.label}
        ${this.iconPosition === "right" ? iconEl : null}
      </button>
    `;
  }
}
