import { expect } from "chai";
import BN from "bn.js";
import { buyBaseInputInternal } from "../sdk/buy";
import { PublicKey } from "@solana/web3.js";

describe("buyBaseInput with fees", () => {
  it("should compute quote + fees + slippage correctly", () => {
    // Example pool reserves
    const baseReserve = new BN(1_000_000);
    const quoteReserve = new BN(2_000_000);

    // Request to buy 10,000 base tokens
    const base = new BN(10_000);

    // Slippage = 1% (slippage=1 => 1%)
    const slippage = 1;

    const result = buyBaseInputInternal(
      base,
      slippage,
      baseReserve,
      quoteReserve,
      {
        admin: PublicKey.unique(),
        lpFeeBasisPoints: new BN(30),
        protocolFeeBasisPoints: new BN(20),
        disableFlags: 0,
        protocolFeeRecipients: [],
        coinCreatorFeeBasisPoints: new BN(0),
        adminSetCoinCreatorAuthority: PublicKey.unique(),
      },
      PublicKey.default,
    );

    console.log("quote =", result.uiQuote.toString());
    console.log("maxQuote =", result.maxQuote.toString());

    // You can calculate offline and replace these with your
    // actual expected values:
    const expectedQuote = new BN(20305); // Example only
    const expectedMaxQuote = new BN(20508); // Example only

    expect(result.uiQuote.toString()).eq(expectedQuote.toString());
    expect(result.maxQuote.toString()).eq(expectedMaxQuote.toString());
  });

  it("should fail if base > baseReserve", () => {
    const baseReserve = new BN(1_000_000);
    const quoteReserve = new BN(2_000_000);
    const base = new BN(2_000_000); // more than pool
    const slippage = 1;

    expect(() =>
      buyBaseInputInternal(
        base,
        slippage,
        baseReserve,
        quoteReserve,
        {
          admin: PublicKey.unique(),
          lpFeeBasisPoints: new BN(30),
          protocolFeeBasisPoints: new BN(20),
          disableFlags: 0,
          protocolFeeRecipients: [],
          coinCreatorFeeBasisPoints: new BN(0),
          adminSetCoinCreatorAuthority: PublicKey.unique(),
        },
        PublicKey.default,
      ),
    ).to.throw("Cannot buy more base tokens than the pool reserves.");
  });
});
