import { BaseService } from "./baseService.server";
import { tryCatch } from "@trigger.dev/core/utils";
import { setSchedulesAddOn } from "~/services/platform.v3.server";
import assertNever from "assert-never";
import { sendToPlain } from "~/utils/plain.server";
import { uiComponent } from "@team-plain/typescript-sdk";

type Input = {
  userId: string;
  organizationId: string;
  action: "purchase" | "quota-increase";
  amount: number;
};

type Result =
  | {
      success: true;
    }
  | {
      success: false;
      error: string;
    };

export class SetSchedulesAddOnService extends BaseService {
  async call({ userId, organizationId, action, amount }: Input): Promise<Result> {
    switch (action) {
      case "purchase": {
        const result = await setSchedulesAddOn(organizationId, amount);
        if (!result) {
          return {
            success: false,
            error: "Failed to update schedules",
          };
        }

        switch (result.result) {
          case "success": {
            return { success: true };
          }
          case "error": {
            return { success: false, error: result.error };
          }
          case "max_quota_reached": {
            return {
              success: false,
              error: `You can't purchase more than ${result.maxQuota} schedules without requesting an increase.`,
            };
          }
          default: {
            return {
              success: false,
              error: "Failed to update schedules, unknown result.",
            };
          }
        }
      }
      case "quota-increase": {
        const user = await this._replica.user.findFirst({
          where: { id: userId },
        });

        if (!user) {
          return { success: false, error: "No matching user found." };
        }

        const organization = await this._replica.organization.findFirst({
          select: { title: true },
          where: { id: organizationId },
        });

        const [error] = await tryCatch(
          sendToPlain({
            userId,
            email: user.email,
            name: user.name ?? user.displayName ?? user.email,
            title: `Schedules quota request: ${amount}`,
            components: [
              uiComponent.text({
                text: `Org: ${organization?.title} (${organizationId})`,
              }),
              uiComponent.divider({ spacingSize: "M" }),
              uiComponent.text({
                text: `Total schedules requested: ${amount}`,
              }),
            ],
          })
        );

        if (error) {
          return { success: false, error: error.message };
        }

        return { success: true };
      }
      default: {
        assertNever(action);
      }
    }
  }
}
