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

-- CreateTable
CREATE TABLE "users" (
    "id" TEXT NOT NULL,
    "email" TEXT NOT NULL,
    "email_verified" TIMESTAMP(3),
    "name" TEXT,
    "phone" VARCHAR(50),
    "phone_verified" BOOLEAN NOT NULL DEFAULT false,
    "phone_verified_at" TIMESTAMP(3),
    "image" TEXT,
    "preferred_currency" VARCHAR(3) NOT NULL DEFAULT 'ARS',
    "whatsapp_enabled" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "accounts" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "provider_account_id" TEXT NOT NULL,
    "refresh_token" TEXT,
    "access_token" TEXT,
    "expires_at" INTEGER,
    "token_type" TEXT,
    "scope" TEXT,
    "id_token" TEXT,
    "session_state" TEXT,

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

-- CreateTable
CREATE TABLE "sessions" (
    "id" TEXT NOT NULL,
    "session_token" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "verification_tokens" (
    "identifier" TEXT NOT NULL,
    "token" TEXT NOT NULL,
    "expires" TIMESTAMP(3) NOT NULL
);

-- CreateTable
CREATE TABLE "searches" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    "contact_phone" VARCHAR(50) NOT NULL,
    "contact_email" VARCHAR(255) NOT NULL,
    "max_budget" DECIMAL(10,2) NOT NULL,
    "currency" VARCHAR(3) NOT NULL,
    "allow_budget_buffer" BOOLEAN NOT NULL DEFAULT true,
    "property_type" VARCHAR(20) NOT NULL,
    "min_rooms" INTEGER NOT NULL,
    "min_covered_area" INTEGER,
    "search_area" TEXT,
    "max_age" INTEGER,
    "amenity_apto_profesional" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_amoblado" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_seguridad" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_balcon_terraza" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_baulera" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_lavadero" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_jardin" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_cochera" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_vestidor" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_gas_natural" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_parrilla" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "amenity_losa_radiante" VARCHAR(20) NOT NULL DEFAULT 'INDIFERENT',
    "work_modality" TEXT[],
    "works_in_caba" BOOLEAN,
    "number_of_tenants" INTEGER,
    "avoid_elevators" BOOLEAN,
    "avoid_high_floors" BOOLEAN,
    "dream_property_desc" TEXT,
    "completeness" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "properties" (
    "id" TEXT NOT NULL,
    "external_id" VARCHAR(255),
    "source" VARCHAR(50) NOT NULL,
    "url" TEXT NOT NULL,
    "title" VARCHAR(500),
    "description" TEXT,
    "price" DECIMAL(10,2) NOT NULL,
    "currency" VARCHAR(3) NOT NULL,
    "expenses" DECIMAL(10,2),
    "property_type" VARCHAR(20),
    "rooms" INTEGER,
    "covered_area" INTEGER,
    "total_area" INTEGER,
    "latitude" DECIMAL(10,8),
    "longitude" DECIMAL(11,8),
    "address" VARCHAR(500),
    "neighborhood" VARCHAR(255),
    "city" VARCHAR(255),
    "age" INTEGER,
    "apto_profesional" BOOLEAN NOT NULL DEFAULT false,
    "amoblado" BOOLEAN NOT NULL DEFAULT false,
    "seguridad" BOOLEAN NOT NULL DEFAULT false,
    "balcon_terraza" BOOLEAN NOT NULL DEFAULT false,
    "baulera" BOOLEAN NOT NULL DEFAULT false,
    "lavadero" BOOLEAN NOT NULL DEFAULT false,
    "jardin" BOOLEAN NOT NULL DEFAULT false,
    "cochera" BOOLEAN NOT NULL DEFAULT false,
    "vestidor" BOOLEAN NOT NULL DEFAULT false,
    "gas_natural" BOOLEAN NOT NULL DEFAULT false,
    "parrilla" BOOLEAN NOT NULL DEFAULT false,
    "losa_radiante" BOOLEAN NOT NULL DEFAULT false,
    "images" TEXT[],
    "is_active" BOOLEAN NOT NULL DEFAULT true,
    "first_seen_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_seen_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "matches" (
    "id" TEXT NOT NULL,
    "search_id" TEXT NOT NULL,
    "property_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "score" INTEGER NOT NULL,
    "explanation" TEXT,
    "match_percentage" INTEGER,
    "is_favorite" BOOLEAN NOT NULL DEFAULT false,
    "is_ignored" BOOLEAN NOT NULL DEFAULT false,
    "viewed_at" TIMESTAMP(3),
    "matched_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "alerts" (
    "id" TEXT NOT NULL,
    "search_id" TEXT NOT NULL,
    "property_id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "channel" VARCHAR(20) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'SENT',
    "error_message" TEXT,
    "sent_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "match_runs" (
    "id" TEXT NOT NULL,
    "executed_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "duration_ms" INTEGER,
    "searches_processed" INTEGER,
    "properties_processed" INTEGER,
    "matches_created" INTEGER,
    "alerts_sent" INTEGER,
    "status" VARCHAR(20),
    "error_message" TEXT,

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

-- CreateTable
CREATE TABLE "exchange_rates" (
    "date" DATE NOT NULL,
    "usd_to_ars" DECIMAL(10,4) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "exchange_rates_pkey" PRIMARY KEY ("date")
);

-- CreateTable
CREATE TABLE "verification_codes" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "phone" VARCHAR(50) NOT NULL,
    "code" VARCHAR(10) NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "verified_at" TIMESTAMP(3),
    "attempts" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "event_log" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "event_type" TEXT NOT NULL,
    "actor_user_id" UUID,
    "entity_type" TEXT,
    "entity_id" UUID,
    "source" TEXT NOT NULL DEFAULT 'app',
    "idempotency_key" TEXT NOT NULL,
    "payload" JSONB NOT NULL DEFAULT '{}',
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "event_outbox" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "event_log_id" UUID NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "attempts" INTEGER NOT NULL DEFAULT 0,
    "next_retry_at" TIMESTAMPTZ,
    "last_error" TEXT,
    "locked_at" TIMESTAMPTZ,
    "locked_by" TEXT,
    "sent_at" TIMESTAMPTZ,
    "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");

-- CreateIndex
CREATE UNIQUE INDEX "accounts_provider_provider_account_id_key" ON "accounts"("provider", "provider_account_id");

-- CreateIndex
CREATE UNIQUE INDEX "sessions_session_token_key" ON "sessions"("session_token");

-- CreateIndex
CREATE UNIQUE INDEX "verification_tokens_token_key" ON "verification_tokens"("token");

-- CreateIndex
CREATE UNIQUE INDEX "verification_tokens_identifier_token_key" ON "verification_tokens"("identifier", "token");

-- CreateIndex
CREATE INDEX "searches_user_id_idx" ON "searches"("user_id");

-- CreateIndex
CREATE INDEX "searches_status_idx" ON "searches"("status");

-- CreateIndex
CREATE UNIQUE INDEX "properties_external_id_key" ON "properties"("external_id");

-- CreateIndex
CREATE INDEX "properties_external_id_idx" ON "properties"("external_id");

-- CreateIndex
CREATE INDEX "properties_latitude_longitude_idx" ON "properties"("latitude", "longitude");

-- CreateIndex
CREATE INDEX "properties_price_idx" ON "properties"("price");

-- CreateIndex
CREATE INDEX "properties_is_active_idx" ON "properties"("is_active");

-- CreateIndex
CREATE INDEX "properties_created_at_idx" ON "properties"("created_at" DESC);

-- CreateIndex
CREATE INDEX "matches_search_id_score_idx" ON "matches"("search_id", "score" DESC);

-- CreateIndex
CREATE INDEX "matches_user_id_idx" ON "matches"("user_id");

-- CreateIndex
CREATE INDEX "matches_property_id_idx" ON "matches"("property_id");

-- CreateIndex
CREATE UNIQUE INDEX "matches_search_id_property_id_key" ON "matches"("search_id", "property_id");

-- CreateIndex
CREATE INDEX "alerts_search_id_idx" ON "alerts"("search_id");

-- CreateIndex
CREATE INDEX "alerts_user_id_idx" ON "alerts"("user_id");

-- CreateIndex
CREATE INDEX "alerts_sent_at_idx" ON "alerts"("sent_at" DESC);

-- CreateIndex
CREATE INDEX "match_runs_executed_at_idx" ON "match_runs"("executed_at" DESC);

-- CreateIndex
CREATE INDEX "verification_codes_user_id_idx" ON "verification_codes"("user_id");

-- CreateIndex
CREATE INDEX "verification_codes_phone_code_idx" ON "verification_codes"("phone", "code");

-- CreateIndex
CREATE UNIQUE INDEX "event_log_idempotency_key_key" ON "event_log"("idempotency_key");

-- CreateIndex
CREATE INDEX "event_log_event_type_created_at_idx" ON "event_log"("event_type", "created_at" DESC);

-- CreateIndex
CREATE INDEX "event_log_entity_type_entity_id_idx" ON "event_log"("entity_type", "entity_id");

-- CreateIndex
CREATE UNIQUE INDEX "event_outbox_event_log_id_key" ON "event_outbox"("event_log_id");

-- CreateIndex
CREATE INDEX "event_outbox_status_next_retry_at_idx" ON "event_outbox"("status", "next_retry_at");

-- CreateIndex
CREATE INDEX "event_outbox_created_at_idx" ON "event_outbox"("created_at");

-- AddForeignKey
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "searches" ADD CONSTRAINT "searches_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "matches" ADD CONSTRAINT "matches_search_id_fkey" FOREIGN KEY ("search_id") REFERENCES "searches"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "matches" ADD CONSTRAINT "matches_property_id_fkey" FOREIGN KEY ("property_id") REFERENCES "properties"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "matches" ADD CONSTRAINT "matches_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "alerts" ADD CONSTRAINT "alerts_search_id_fkey" FOREIGN KEY ("search_id") REFERENCES "searches"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "alerts" ADD CONSTRAINT "alerts_property_id_fkey" FOREIGN KEY ("property_id") REFERENCES "properties"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "alerts" ADD CONSTRAINT "alerts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "verification_codes" ADD CONSTRAINT "verification_codes_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "event_outbox" ADD CONSTRAINT "event_outbox_event_log_id_fkey" FOREIGN KEY ("event_log_id") REFERENCES "event_log"("id") ON DELETE CASCADE ON UPDATE CASCADE;

