/*
  Warnings:

  - You are about to drop the column `authorization_code` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `bank` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `brand` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `card_type` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `country_code` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `exp_month` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `exp_year` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `is_default` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `last4` on the `billing_methods` table. All the data in the column will be lost.
  - You are about to drop the column `billing_method_id` on the `payments` table. All the data in the column will be lost.
  - You are about to drop the column `channel` on the `payments` table. All the data in the column will be lost.
  - You are about to drop the column `metadata` on the `payments` table. All the data in the column will be lost.
  - You are about to drop the column `paystack_ref` on the `payments` table. All the data in the column will be lost.
  - You are about to drop the column `status` on the `payments` table. All the data in the column will be lost.
  - You are about to drop the column `transaction_ref` on the `payments` table. All the data in the column will be lost.
  - You are about to alter the column `amount` on the `payments` table. The data in that column could be lost. The data in that column will be cast from `Decimal(10,2)` to `DoublePrecision`.
  - You are about to drop the column `subscription_plan_id` on the `subscriptions` table. All the data in the column will be lost.
  - You are about to drop the `subscription_plans` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[txnId]` on the table `payments` will be added. If there are existing duplicate values, this will fail.
  - A unique constraint covering the columns `[reference]` on the table `payments` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `reference` to the `payments` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE "payments" DROP CONSTRAINT "payments_billing_method_id_fkey";

-- DropForeignKey
ALTER TABLE "subscriptions" DROP CONSTRAINT "subscriptions_payment_id_fkey";

-- DropForeignKey
ALTER TABLE "subscriptions" DROP CONSTRAINT "subscriptions_subscription_plan_id_fkey";

-- DropIndex
DROP INDEX "billing_methods_authorization_code_key";

-- DropIndex
DROP INDEX "payments_billing_method_id_idx";

-- DropIndex
DROP INDEX "payments_paystack_ref_key";

-- DropIndex
DROP INDEX "payments_status_idx";

-- DropIndex
DROP INDEX "payments_transaction_ref_key";

-- DropIndex
DROP INDEX "subscriptions_subscription_plan_id_idx";

-- AlterTable
ALTER TABLE "billing_methods" DROP COLUMN "authorization_code",
DROP COLUMN "bank",
DROP COLUMN "brand",
DROP COLUMN "card_type",
DROP COLUMN "country_code",
DROP COLUMN "exp_month",
DROP COLUMN "exp_year",
DROP COLUMN "is_default",
DROP COLUMN "last4";

-- AlterTable
ALTER TABLE "payments" DROP COLUMN "billing_method_id",
DROP COLUMN "channel",
DROP COLUMN "metadata",
DROP COLUMN "paystack_ref",
DROP COLUMN "status",
DROP COLUMN "transaction_ref",
ADD COLUMN     "access_code" TEXT,
ADD COLUMN     "authorization_url" TEXT,
ADD COLUMN     "card_id" TEXT,
ADD COLUMN     "failed_at" TIMESTAMP(3),
ADD COLUMN     "plan_id" TEXT,
ADD COLUMN     "reference" TEXT NOT NULL,
ADD COLUMN     "refunded_at" TIMESTAMP(3),
ADD COLUMN     "txnId" TEXT,
ALTER COLUMN "amount" SET DATA TYPE DOUBLE PRECISION,
ALTER COLUMN "currency" DROP DEFAULT;

-- AlterTable
ALTER TABLE "subscriptions" DROP COLUMN "subscription_plan_id";

-- DropTable
DROP TABLE "subscription_plans";

-- CreateTable
CREATE TABLE "plans" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "headline" TEXT,
    "description" TEXT,
    "price" DOUBLE PRECISION NOT NULL,
    "currency" TEXT NOT NULL,
    "interval" TEXT NOT NULL,
    "trial_duration" INTEGER,
    "permissions" JSONB,
    "is_active" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "plans_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "news" (
    "id" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "image" TEXT,
    "excerpt" TEXT,
    "url" TEXT NOT NULL,

    CONSTRAINT "news_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "plans_name_key" ON "plans"("name");

-- CreateIndex
CREATE INDEX "news_title_idx" ON "news"("title");

-- CreateIndex
CREATE UNIQUE INDEX "payments_txnId_key" ON "payments"("txnId");

-- CreateIndex
CREATE UNIQUE INDEX "payments_reference_key" ON "payments"("reference");

-- CreateIndex
CREATE INDEX "payments_card_id_idx" ON "payments"("card_id");

-- AddForeignKey
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_payment_id_fkey" FOREIGN KEY ("payment_id") REFERENCES "payments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_plan_id_fkey" FOREIGN KEY ("plan_id") REFERENCES "plans"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "payments" ADD CONSTRAINT "payments_card_id_fkey" FOREIGN KEY ("card_id") REFERENCES "billing_methods"("id") ON DELETE SET NULL ON UPDATE CASCADE;
