
-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "rebreak";

-- CreateEnum
CREATE TYPE "rebreak"."FeedbackStatus" AS ENUM ('PENDING', 'REVIEWING', 'PLANNED', 'SHIPPED', 'REJECTED');

-- CreateEnum
CREATE TYPE "rebreak"."GameChallengeStatus" AS ENUM ('OPEN', 'ACTIVE', 'FINISHED', 'CANCELLED');

-- CreateTable
CREATE TABLE "rebreak"."profiles" (
    "id" UUID NOT NULL,
    "username" TEXT,
    "nickname" TEXT,
    "avatar" TEXT,
    "plan" TEXT NOT NULL DEFAULT 'free',
    "streak" INTEGER NOT NULL DEFAULT 0,
    "followers_count" INTEGER NOT NULL DEFAULT 0,
    "stripe_customer_id" TEXT,
    "stripe_subscription_id" TEXT,
    "premium_until" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."streaks" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "start_date" DATE NOT NULL,
    "current_days" INTEGER NOT NULL DEFAULT 0,
    "longest_days" INTEGER NOT NULL DEFAULT 0,
    "avg_monthly_savings" DOUBLE PRECISION,
    "is_active" BOOLEAN NOT NULL DEFAULT true,

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

-- CreateTable
CREATE TABLE "rebreak"."streak_events" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "type" TEXT NOT NULL,
    "meta" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."urge_logs" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "emotion" TEXT NOT NULL,
    "was_overcome" BOOLEAN NOT NULL DEFAULT false,
    "breathing_done" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "rebreak"."community_posts" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "category" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "image_url" TEXT,
    "upvotes" INTEGER NOT NULL DEFAULT 0,
    "likes_count" INTEGER NOT NULL DEFAULT 0,
    "dislikes_count" INTEGER NOT NULL DEFAULT 0,
    "comments_count" INTEGER NOT NULL DEFAULT 0,
    "reposts_count" INTEGER NOT NULL DEFAULT 0,
    "is_anonymous" BOOLEAN NOT NULL DEFAULT false,
    "is_moderated" BOOLEAN NOT NULL DEFAULT false,
    "repost_of_id" UUID,
    "challenge_id" UUID,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."post_likes" (
    "user_id" UUID NOT NULL,
    "post_id" UUID NOT NULL,
    "type" TEXT NOT NULL,

    CONSTRAINT "post_likes_pkey" PRIMARY KEY ("user_id","post_id")
);

-- CreateTable
CREATE TABLE "rebreak"."community_replies" (
    "id" UUID NOT NULL,
    "post_id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "parent_reply_id" UUID,
    "is_anonymous" BOOLEAN NOT NULL DEFAULT false,
    "likes_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."comment_likes" (
    "user_id" UUID NOT NULL,
    "comment_id" UUID NOT NULL,

    CONSTRAINT "comment_likes_pkey" PRIMARY KEY ("user_id","comment_id")
);

-- CreateTable
CREATE TABLE "rebreak"."chat_messages" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "room_id" UUID,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."direct_messages" (
    "id" UUID NOT NULL,
    "sender_id" UUID NOT NULL,
    "receiver_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "read_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "rebreak"."user_follows" (
    "follower_id" UUID NOT NULL,
    "following_id" UUID NOT NULL,

    CONSTRAINT "user_follows_pkey" PRIMARY KEY ("follower_id","following_id")
);

-- CreateTable
CREATE TABLE "rebreak"."user_scores" (
    "user_id" UUID NOT NULL,
    "total_points" INTEGER NOT NULL DEFAULT 0,
    "tier" TEXT NOT NULL DEFAULT 'beginner',
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "user_scores_pkey" PRIMARY KEY ("user_id")
);

-- CreateTable
CREATE TABLE "rebreak"."score_events" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "event_type" TEXT NOT NULL,
    "points" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "meta" JSONB,

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

-- CreateTable
CREATE TABLE "rebreak"."user_custom_domains" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "domain" TEXT NOT NULL,
    "source" TEXT NOT NULL DEFAULT 'manual',
    "status" TEXT NOT NULL DEFAULT 'active',
    "post_id" UUID,
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."domain_submissions" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "domain" TEXT NOT NULL,
    "custom_domain_id" UUID NOT NULL,
    "post_id" UUID,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "yes_votes" INTEGER NOT NULL DEFAULT 0,
    "no_votes" INTEGER NOT NULL DEFAULT 0,
    "review_note" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "reviewed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "rebreak"."domain_votes" (
    "user_id" UUID NOT NULL,
    "submission_id" UUID NOT NULL,
    "vote" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "domain_votes_pkey" PRIMARY KEY ("user_id","submission_id")
);

-- CreateTable
CREATE TABLE "rebreak"."feedback_items" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "content" TEXT NOT NULL,
    "category" TEXT,
    "status" "rebreak"."FeedbackStatus" NOT NULL DEFAULT 'PENDING',
    "admin_note" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "rebreak"."blocklist_domains" (
    "id" UUID NOT NULL,
    "domain" TEXT NOT NULL,
    "source" TEXT NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "report_count" INTEGER NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "rebreak"."trusted_contacts" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "name" TEXT NOT NULL,
    "phone" TEXT,
    "email" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."coach_sessions" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "content" JSONB NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."mail_connections" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "email" TEXT NOT NULL,
    "provider" TEXT NOT NULL DEFAULT 'imap',
    "provider_name" TEXT,
    "imap_host" TEXT NOT NULL,
    "imap_port" INTEGER NOT NULL,
    "password_encrypted" TEXT NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "scan_interval" INTEGER NOT NULL DEFAULT 24,
    "last_scanned_at" TIMESTAMP(3),
    "next_scan_at" TIMESTAMP(3),
    "emails_blocked" INTEGER NOT NULL DEFAULT 0,
    "emails_scanned" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."game_challenges" (
    "id" UUID NOT NULL,
    "challenger_id" UUID NOT NULL,
    "challenger_name" TEXT NOT NULL,
    "opponent_id" UUID,
    "opponent_name" TEXT,
    "status" "rebreak"."GameChallengeStatus" NOT NULL DEFAULT 'OPEN',
    "board" TEXT NOT NULL DEFAULT '---------',
    "current_turn" TEXT NOT NULL DEFAULT 'X',
    "winner" TEXT,
    "post_id" UUID,
    "game_type" TEXT NOT NULL DEFAULT 'tictactoe',
    "is_live" BOOLEAN NOT NULL DEFAULT false,
    "memory_state" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "rebreak"."notifications" (
    "id" UUID NOT NULL,
    "recipient_id" UUID NOT NULL,
    "type" TEXT NOT NULL,
    "actor_name" TEXT NOT NULL,
    "post_id" UUID,
    "preview" TEXT,
    "read_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."game_scores" (
    "user_id" UUID NOT NULL,
    "player_name" TEXT NOT NULL,
    "wins" INTEGER NOT NULL DEFAULT 0,
    "losses" INTEGER NOT NULL DEFAULT 0,
    "draws" INTEGER NOT NULL DEFAULT 0,
    "points" INTEGER NOT NULL DEFAULT 0,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "game_scores_pkey" PRIMARY KEY ("user_id")
);

-- CreateTable
CREATE TABLE "rebreak"."mail_blocked" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "connection_id" UUID NOT NULL,
    "gmail_message_id" TEXT NOT NULL,
    "sender_email" TEXT NOT NULL,
    "sender_name" TEXT,
    "subject" TEXT NOT NULL,
    "received_at" TIMESTAMP(3) NOT NULL,
    "action" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "rebreak"."imap_proxy_accounts" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "proxy_username" TEXT NOT NULL,
    "proxy_password" TEXT NOT NULL,
    "connection_id" UUID NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "user_custom_domains_user_id_domain_key" ON "rebreak"."user_custom_domains"("user_id", "domain");

-- CreateIndex
CREATE UNIQUE INDEX "domain_submissions_custom_domain_id_key" ON "rebreak"."domain_submissions"("custom_domain_id");

-- CreateIndex
CREATE UNIQUE INDEX "blocklist_domains_domain_key" ON "rebreak"."blocklist_domains"("domain");

-- CreateIndex
CREATE UNIQUE INDEX "mail_connections_user_id_email_key" ON "rebreak"."mail_connections"("user_id", "email");

-- CreateIndex
CREATE INDEX "notifications_recipient_id_read_at_idx" ON "rebreak"."notifications"("recipient_id", "read_at");

-- CreateIndex
CREATE UNIQUE INDEX "mail_blocked_gmail_message_id_user_id_key" ON "rebreak"."mail_blocked"("gmail_message_id", "user_id");

-- CreateIndex
CREATE UNIQUE INDEX "imap_proxy_accounts_proxy_username_key" ON "rebreak"."imap_proxy_accounts"("proxy_username");

-- CreateIndex
CREATE UNIQUE INDEX "imap_proxy_accounts_connection_id_key" ON "rebreak"."imap_proxy_accounts"("connection_id");

-- AddForeignKey
ALTER TABLE "rebreak"."community_posts" ADD CONSTRAINT "community_posts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "rebreak"."profiles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."community_posts" ADD CONSTRAINT "community_posts_repost_of_id_fkey" FOREIGN KEY ("repost_of_id") REFERENCES "rebreak"."community_posts"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."post_likes" ADD CONSTRAINT "post_likes_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "rebreak"."community_posts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."community_replies" ADD CONSTRAINT "community_replies_post_id_fkey" FOREIGN KEY ("post_id") REFERENCES "rebreak"."community_posts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."community_replies" ADD CONSTRAINT "community_replies_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "rebreak"."profiles"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."comment_likes" ADD CONSTRAINT "comment_likes_comment_id_fkey" FOREIGN KEY ("comment_id") REFERENCES "rebreak"."community_replies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."domain_submissions" ADD CONSTRAINT "domain_submissions_custom_domain_id_fkey" FOREIGN KEY ("custom_domain_id") REFERENCES "rebreak"."user_custom_domains"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."domain_votes" ADD CONSTRAINT "domain_votes_submission_id_fkey" FOREIGN KEY ("submission_id") REFERENCES "rebreak"."domain_submissions"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rebreak"."mail_blocked" ADD CONSTRAINT "mail_blocked_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "rebreak"."mail_connections"("id") ON DELETE CASCADE ON UPDATE CASCADE;

