import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import WeekendTimeSelector from "./WeekendTimeSelector";

const mockOnConfirm = vi.fn();
const mockOnClose = vi.fn();

function renderSelector(props = {}) {
  return render(
    <WeekendTimeSelector
      onConfirm={mockOnConfirm}
      onClose={mockOnClose}
      {...props}
    />,
  );
}

describe("WeekendTimeSelector", () => {
  it("renders time presets", () => {
    renderSelector();
    expect(screen.getByText("周六全天")).toBeInTheDocument();
    expect(screen.getByText("周日全天")).toBeInTheDocument();
    expect(screen.getByText("整个周末")).toBeInTheDocument();
  });

  it("renders hour selectors", () => {
    renderSelector();
    const selects = screen.getAllByRole("combobox");
    expect(selects).toHaveLength(2);
  });

  it("shows confirm button", () => {
    renderSelector();
    expect(screen.getByText("生成周末计划")).toBeInTheDocument();
  });

  it("calls onConfirm with default config", () => {
    renderSelector();
    fireEvent.click(screen.getByText("生成周末计划"));
    expect(mockOnConfirm).toHaveBeenCalledWith({
      date: "周六",
      startHour: 10,
      endHour: 21,
    });
  });

  it("calls onClose when close button clicked", () => {
    renderSelector();
    fireEvent.click(screen.getByLabelText("关闭"));
    expect(mockOnClose).toHaveBeenCalled();
  });

  it("switches preset on click", () => {
    renderSelector();
    fireEvent.click(screen.getByText("周日全天"));
    fireEvent.click(screen.getByText("生成周末计划"));
    expect(mockOnConfirm).toHaveBeenCalledWith({
      date: "周日",
      startHour: 10,
      endHour: 21,
    });
  });
});
