import Shortify from "../src";
import dotenv from "dotenv";

// Load environment variables
dotenv.config();

async function main() {
  try {
    // Create a new Shortify instance
    const shortify = new Shortify(
      "https://short.example.com/",
      process.env.MONGODB_URI || "mongodb://localhost:27017/shortify"
    );

    console.log("Connecting to MongoDB...");
    await shortify.connect();
    console.log("Connected to MongoDB");

    // Shorten a URL
    const url = "https://example.com/very-long-url-that-needs-shortening";
    console.log(`Shortening URL: ${url}`);
    const shortened = await shortify.shorten(url);
    console.log(
      `Shortened URL: ${shortened.shortUrl} (ID: ${shortened.urlId})`
    );

    // Resolve the shortened URL
    console.log(`Resolving URL ID: ${shortened.urlId}`);
    const original = await shortify.resolve(shortened.urlId);
    console.log(`Original URL: ${original}`);

    // Get stats for the shortened URL
    console.log(`Getting stats for URL ID: ${shortened.urlId}`);
    const stats = await shortify.getStats(shortened.urlId);
    console.log("Stats:", stats);

    // Shorten with custom options
    console.log("\nShortening URL with custom options:");
    const customShortened = await shortify.shorten("https://example.org", {
      customUrlId: "my-custom-id",
      expiresInDays: 7,
    });
    console.log(`Custom shortened URL: ${customShortened.shortUrl}`);
    console.log(`Expires at: ${customShortened.expiresAt}`);

    // Clean up
    console.log("\nCleaning up...");
    await shortify.delete(shortened.urlId);
    await shortify.delete("my-custom-id");
    console.log("URLs deleted");

    // Disconnect
    console.log("Disconnecting from MongoDB...");
    await shortify.disconnect();
    console.log("Disconnected from MongoDB");
  } catch (error) {
    console.error("Error:", error);
  }
}

// Run the example
main();
