-- Complete fresh start migration
-- Drop all existing tables and types
DROP TABLE IF EXISTS "shopping_list_items" CASCADE;
DROP TABLE IF EXISTS "auth_users" CASCADE;
DROP TABLE IF EXISTS "inventory_items" CASCADE;
DROP TABLE IF EXISTS "memberships" CASCADE;
DROP TABLE IF EXISTS "activity_log" CASCADE;
DROP TABLE IF EXISTS "alerts" CASCADE;
DROP TABLE IF EXISTS "ai_analyses" CASCADE;
DROP TABLE IF EXISTS "organizations" CASCADE;
DROP TABLE IF EXISTS "pickup_slots" CASCADE;
DROP TABLE IF EXISTS "order_items" CASCADE;
DROP TABLE IF EXISTS "orders" CASCADE;
DROP TABLE IF EXISTS "stock_analysis" CASCADE;
DROP TABLE IF EXISTS "pantry_items" CASCADE;
DROP TABLE IF EXISTS "categories" CASCADE;
DROP TABLE IF EXISTS "users" CASCADE;

-- Drop all types
DROP TYPE IF EXISTS "public"."alert_type" CASCADE;
DROP TYPE IF EXISTS "public"."item_condition" CASCADE;
DROP TYPE IF EXISTS "public"."order_status" CASCADE;
DROP TYPE IF EXISTS "public"."org_role" CASCADE;
DROP TYPE IF EXISTS "public"."org_status" CASCADE;
DROP TYPE IF EXISTS "public"."platform_role" CASCADE;

-- Create fresh tables
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" varchar(255) NOT NULL,
	"full_name" varchar(255),
	"role" varchar(50) DEFAULT 'user' NOT NULL,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "users_email_unique" UNIQUE("email")
);

CREATE TABLE "categories" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" varchar(100) NOT NULL,
	"description" text,
	"color" varchar(7),
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "categories_name_unique" UNIQUE("name")
);

CREATE TABLE "pantry_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"category_id" uuid,
	"name" varchar(255) NOT NULL,
	"description" text,
	"quantity" integer DEFAULT 0 NOT NULL,
	"unit" varchar(50),
	"expiry_date" timestamp,
	"purchase_date" timestamp DEFAULT now() NOT NULL,
	"price" numeric(10, 2),
	"location" varchar(100),
	"is_low_stock" boolean DEFAULT false,
	"low_stock_threshold" integer DEFAULT 5,
	"barcode" varchar(50),
	"image_url" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);

CREATE TABLE "orders" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"name" varchar(255) NOT NULL,
	"quantity" integer DEFAULT 1 NOT NULL,
	"unit" varchar(50),
	"is_purchased" boolean DEFAULT false,
	"priority" varchar(20) DEFAULT 'medium',
	"notes" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);

CREATE TABLE "order_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"order_id" uuid NOT NULL,
	"pantry_item_id" uuid,
	"name" varchar(255) NOT NULL,
	"quantity" integer DEFAULT 1 NOT NULL,
	"unit" varchar(50),
	"created_at" timestamp DEFAULT now() NOT NULL
);

CREATE TABLE "stock_analysis" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"analysis_type" varchar(100) NOT NULL,
	"data" json NOT NULL,
	"insights" text,
	"recommendations" text,
	"confidence" numeric(3, 2),
	"created_at" timestamp DEFAULT now() NOT NULL
);

-- Add foreign keys
ALTER TABLE "pantry_items" ADD CONSTRAINT "pantry_items_category_id_categories_id_fk" 
  FOREIGN KEY ("category_id") REFERENCES "public"."categories"("id") ON DELETE no action ON UPDATE no action;

ALTER TABLE "orders" ADD CONSTRAINT "orders_user_id_users_id_fk" 
  FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;

ALTER TABLE "order_items" ADD CONSTRAINT "order_items_order_id_orders_id_fk" 
  FOREIGN KEY ("order_id") REFERENCES "public"."orders"("id") ON DELETE no action ON UPDATE no action;

ALTER TABLE "order_items" ADD CONSTRAINT "order_items_pantry_item_id_pantry_items_id_fk" 
  FOREIGN KEY ("pantry_item_id") REFERENCES "public"."pantry_items"("id") ON DELETE no action ON UPDATE no action;

ALTER TABLE "stock_analysis" ADD CONSTRAINT "stock_analysis_user_id_users_id_fk" 
  FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;

