CREATE TABLE "senses" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"word_id" uuid NOT NULL,
	"sense_index" smallint DEFAULT 0 NOT NULL,
	"difficulty" varchar(20) NOT NULL,
	"definitions" text[] DEFAULT '{}' NOT NULL,
	"examples" text[] DEFAULT '{}' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "unique_sense_per_word" UNIQUE("word_id","sense_index"),
	CONSTRAINT "senses_difficulty_check" CHECK ("senses"."difficulty" IN ('easy', 'medium', 'hard'))
);
--> statement-breakpoint
CREATE TABLE "translations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"sense_id" uuid NOT NULL,
	"target_language_code" varchar(10) NOT NULL,
	"translation" text NOT NULL,
	"gender" varchar(20),
	"difficulty" varchar(20) NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "unique_translation_per_sense" UNIQUE("sense_id","target_language_code","translation"),
	CONSTRAINT "translations_language_code_check" CHECK ("translations"."target_language_code" IN ('en', 'it', 'de', 'fr', 'es')),
	CONSTRAINT "translations_difficulty_check" CHECK ("translations"."difficulty" IN ('easy', 'medium', 'hard')),
	CONSTRAINT "translations_gender_check" CHECK ("translations"."gender" IS NULL OR "translations"."gender" IN ('masculine', 'feminine', 'neuter'))
);
--> statement-breakpoint
CREATE TABLE "words" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"headword" text NOT NULL,
	"language_code" varchar(10) NOT NULL,
	"pos" varchar(20) NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "unique_word_per_language_and_pos" UNIQUE("headword","language_code","pos"),
	CONSTRAINT "words_language_code_check" CHECK ("words"."language_code" IN ('en', 'it', 'de', 'fr', 'es')),
	CONSTRAINT "words_pos_check" CHECK ("words"."pos" IN ('noun', 'verb', 'adjective', 'adverb'))
);
--> statement-breakpoint
ALTER TABLE "entry_translations" DROP CONSTRAINT "difficulty_check";--> statement-breakpoint
ALTER TABLE "vocabulary_entries" DROP CONSTRAINT "difficulty_check";--> statement-breakpoint
ALTER TABLE "senses" ADD CONSTRAINT "senses_word_id_words_id_fk" FOREIGN KEY ("word_id") REFERENCES "public"."words"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "translations" ADD CONSTRAINT "translations_sense_id_senses_id_fk" FOREIGN KEY ("sense_id") REFERENCES "public"."senses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_word_sense_difficulty" ON "senses" USING btree ("word_id","difficulty");--> statement-breakpoint
CREATE INDEX "idx_translations_sense_language_difficulty" ON "translations" USING btree ("sense_id","target_language_code","difficulty");--> statement-breakpoint
CREATE INDEX "idx_language_code_pos" ON "words" USING btree ("language_code","pos");--> statement-breakpoint
ALTER TABLE "entry_translations" ADD CONSTRAINT "difficulty_check" CHECK ("entry_translations"."difficulty" IS NULL OR "entry_translations"."difficulty" IN ('easy', 'medium', 'hard'));--> statement-breakpoint
ALTER TABLE "vocabulary_entries" ADD CONSTRAINT "difficulty_check" CHECK ("vocabulary_entries"."difficulty" IS NULL OR "vocabulary_entries"."difficulty" IN ('easy', 'medium', 'hard'));