-- Extended features: refresh tokens, meal planner, favorites, recipe-catalog link
-- Apply after 001–003. Schema: diet

BEGIN;

-- ---------------------------------------------------------------------------
-- Refresh tokens (persist sessions across API restarts)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."RefreshTokens" (
    "Token"           varchar(128) PRIMARY KEY,
    "UserId"          uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
    "ExpiresAtUtc"    timestamptz NOT NULL,
    "CreatedAtUtc"    timestamptz NOT NULL DEFAULT now(),
    "IsRevoked"       boolean NOT NULL DEFAULT false
);

CREATE INDEX IF NOT EXISTS "IX_RefreshTokens_UserId" ON diet."RefreshTokens" ("UserId");
CREATE INDEX IF NOT EXISTS "IX_RefreshTokens_ExpiresAtUtc" ON diet."RefreshTokens" ("ExpiresAtUtc");

-- ---------------------------------------------------------------------------
-- Weekly meal plan (one recipe per meal slot per day)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."MealPlanEntries" (
    "Id"              serial PRIMARY KEY,
    "UserId"          uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
    "PlanDate"        date NOT NULL,
    "MealCategory"    int NOT NULL CHECK ("MealCategory" BETWEEN 0 AND 4),
    "RecipeId"        int NOT NULL REFERENCES diet."Recipes"("Id") ON DELETE CASCADE,
    "Portions"        numeric(4,2) NOT NULL DEFAULT 1 CHECK ("Portions" > 0),
    "CreatedAt"       timestamptz NOT NULL DEFAULT now(),
    "UpdatedAt"       timestamptz NULL,
    UNIQUE ("UserId", "PlanDate", "MealCategory")
);

CREATE INDEX IF NOT EXISTS "IX_MealPlanEntries_UserId_PlanDate"
    ON diet."MealPlanEntries" ("UserId", "PlanDate");

-- ---------------------------------------------------------------------------
-- Favorites (quick logging)
-- ---------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS diet."UserFavoriteRecipes" (
    "UserId"    uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
    "RecipeId"  int NOT NULL REFERENCES diet."Recipes"("Id") ON DELETE CASCADE,
    "CreatedAt" timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY ("UserId", "RecipeId")
);

CREATE TABLE IF NOT EXISTS diet."UserFavoriteCatalogItems" (
    "UserId"        uuid NOT NULL REFERENCES diet."AspNetUsers"("Id") ON DELETE CASCADE,
    "CatalogItemId" int NOT NULL REFERENCES diet."IngredientNutritionCatalog"("Id") ON DELETE CASCADE,
    "CreatedAt"     timestamptz NOT NULL DEFAULT now(),
    PRIMARY KEY ("UserId", "CatalogItemId")
);

-- ---------------------------------------------------------------------------
-- Link recipe ingredients to nutrition catalog (auto macros)
-- ---------------------------------------------------------------------------
ALTER TABLE diet."Ingredients"
    ADD COLUMN IF NOT EXISTS "CatalogItemId" int NULL
    REFERENCES diet."IngredientNutritionCatalog"("Id") ON DELETE SET NULL;

COMMIT;
