CREATE TYPE "public"."language" AS ENUM('sk', 'cs', 'en', 'other');--> statement-breakpoint
CREATE TYPE "public"."priority" AS ENUM('low', 'medium', 'high', 'urgent');--> statement-breakpoint
CREATE TYPE "public"."report_status" AS ENUM('pending', 'in_review', 'approved', 'rejected', 'duplicate');--> statement-breakpoint
CREATE TYPE "public"."role" AS ENUM('admin', 'moderator');--> statement-breakpoint
CREATE TYPE "public"."source_status" AS ENUM('pending', 'verified', 'rejected', 'under_review');--> statement-breakpoint
CREATE TYPE "public"."source_type" AS ENUM('website', 'facebook_page', 'facebook_group', 'instagram', 'blog', 'news_site', 'youtube', 'tiktok', 'telegram', 'other');--> statement-breakpoint
CREATE TABLE "analytics_events" (
	"id" serial PRIMARY KEY NOT NULL,
	"event_type" varchar(50) NOT NULL,
	"source_url" varchar(1000),
	"source_id" integer,
	"user_id" integer,
	"api_key_id" integer,
	"ip_address" varchar(45),
	"user_agent" text,
	"response_time" integer,
	"status_code" integer,
	"metadata" text DEFAULT '{}',
	"timestamp" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "api_keys" (
	"id" serial PRIMARY KEY NOT NULL,
	"key_hash" varchar(255) NOT NULL,
	"name" varchar(100) NOT NULL,
	"description" text,
	"owner_email" varchar(255) NOT NULL,
	"permissions" text DEFAULT '["read"]',
	"rate_limit" integer DEFAULT 1000,
	"is_active" boolean DEFAULT true,
	"usage_count" integer DEFAULT 0,
	"last_used" timestamp,
	"expires_at" timestamp,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "api_keys_key_hash_unique" UNIQUE("key_hash")
);
--> statement-breakpoint
CREATE TABLE "audit_logs" (
	"id" serial PRIMARY KEY NOT NULL,
	"user_id" integer,
	"action" varchar(50) NOT NULL,
	"resource_type" varchar(50) NOT NULL,
	"resource_id" integer,
	"details" text,
	"ip_address" varchar(45),
	"created_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "categories" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" varchar(100) NOT NULL,
	"slug" varchar(100) NOT NULL,
	"description" text,
	"color" varchar(7) DEFAULT '#6B7280',
	"priority" integer DEFAULT 1,
	"icon" varchar(50),
	"is_active" boolean DEFAULT true,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "categories_name_unique" UNIQUE("name"),
	CONSTRAINT "categories_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "email_templates" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" varchar(100) NOT NULL,
	"subject" varchar(200) NOT NULL,
	"html_body" text NOT NULL,
	"text_body" text,
	"variables" text DEFAULT '[]',
	"is_active" boolean DEFAULT true,
	"created_by" integer,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "email_templates_name_unique" UNIQUE("name")
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" serial PRIMARY KEY NOT NULL,
	"title" varchar(200) NOT NULL,
	"message" text NOT NULL,
	"type" varchar(50) DEFAULT 'info',
	"user_id" integer,
	"is_read" boolean DEFAULT false,
	"action_url" varchar(500),
	"metadata" text DEFAULT '{}',
	"created_at" timestamp DEFAULT now(),
	"read_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "reports" (
	"id" serial PRIMARY KEY NOT NULL,
	"source_url" varchar(1000) NOT NULL,
	"source_domain" varchar(255) NOT NULL,
	"reporter_email" varchar(255),
	"reporter_name" varchar(100),
	"category_suggestions" text,
	"description" text NOT NULL,
	"evidence_urls" text,
	"priority" "priority" DEFAULT 'medium',
	"status" "report_status" DEFAULT 'pending',
	"assigned_to" integer,
	"admin_notes" text,
	"processed_at" timestamp,
	"ip_address" varchar(45),
	"user_agent" text,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "scheduled_jobs" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" varchar(100) NOT NULL,
	"type" varchar(50) NOT NULL,
	"schedule" varchar(100) NOT NULL,
	"is_active" boolean DEFAULT true,
	"last_run" timestamp,
	"next_run" timestamp,
	"last_result" varchar(50),
	"config" text DEFAULT '{}',
	"created_by" integer,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "source_analytics" (
	"id" serial PRIMARY KEY NOT NULL,
	"source_id" integer NOT NULL,
	"date" timestamp NOT NULL,
	"lookup_count" integer DEFAULT 0,
	"report_count" integer DEFAULT 0,
	"view_count" integer DEFAULT 0,
	"risk_score" numeric(3, 2),
	"metadata" text DEFAULT '{}'
);
--> statement-breakpoint
CREATE TABLE "source_categories" (
	"id" serial PRIMARY KEY NOT NULL,
	"source_id" integer NOT NULL,
	"category_id" integer NOT NULL,
	"confidence_score" numeric(3, 2) DEFAULT '1.0',
	"added_by" integer,
	"created_at" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "sources" (
	"id" serial PRIMARY KEY NOT NULL,
	"url" varchar(1000) NOT NULL,
	"domain" varchar(255) NOT NULL,
	"title" varchar(500),
	"description" text,
	"type" "source_type" NOT NULL,
	"status" "source_status" DEFAULT 'pending',
	"risk_level" integer DEFAULT 1,
	"language" "language" DEFAULT 'sk',
	"evidence_urls" text,
	"reported_by" varchar(255),
	"verified_by" integer,
	"rejection_reason" text,
	"follower_count" integer DEFAULT 0,
	"last_checked" timestamp,
	"metadata" text DEFAULT '{}',
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "sources_url_unique" UNIQUE("url")
);
--> statement-breakpoint
CREATE TABLE "system_metrics" (
	"id" serial PRIMARY KEY NOT NULL,
	"metric_type" varchar(50) NOT NULL,
	"value" numeric(10, 2) NOT NULL,
	"unit" varchar(20),
	"timestamp" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE TABLE "system_settings" (
	"id" serial PRIMARY KEY NOT NULL,
	"key" varchar(100) NOT NULL,
	"value" text,
	"type" varchar(50) DEFAULT 'string',
	"description" text,
	"category" varchar(50) DEFAULT 'general',
	"is_public" boolean DEFAULT false,
	"updated_by" integer,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "system_settings_key_unique" UNIQUE("key")
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" serial PRIMARY KEY NOT NULL,
	"email" varchar(255) NOT NULL,
	"password_hash" varchar(255) NOT NULL,
	"name" varchar(100) NOT NULL,
	"role" "role" DEFAULT 'moderator',
	"is_active" boolean DEFAULT true,
	"last_login" timestamp,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now(),
	CONSTRAINT "users_email_unique" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "webhooks" (
	"id" serial PRIMARY KEY NOT NULL,
	"name" varchar(100) NOT NULL,
	"url" varchar(1000) NOT NULL,
	"events" text NOT NULL,
	"secret" varchar(255),
	"is_active" boolean DEFAULT true,
	"headers" text DEFAULT '{}',
	"last_triggered" timestamp,
	"success_count" integer DEFAULT 0,
	"failure_count" integer DEFAULT 0,
	"created_by" integer,
	"created_at" timestamp DEFAULT now(),
	"updated_at" timestamp DEFAULT now()
);
--> statement-breakpoint
ALTER TABLE "analytics_events" ADD CONSTRAINT "analytics_events_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "analytics_events" ADD CONSTRAINT "analytics_events_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "analytics_events" ADD CONSTRAINT "analytics_events_api_key_id_api_keys_id_fk" FOREIGN KEY ("api_key_id") REFERENCES "public"."api_keys"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "email_templates" ADD CONSTRAINT "email_templates_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "notifications" ADD CONSTRAINT "notifications_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "reports" ADD CONSTRAINT "reports_assigned_to_users_id_fk" FOREIGN KEY ("assigned_to") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "scheduled_jobs" ADD CONSTRAINT "scheduled_jobs_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_analytics" ADD CONSTRAINT "source_analytics_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_categories" ADD CONSTRAINT "source_categories_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_categories" ADD CONSTRAINT "source_categories_category_id_categories_id_fk" FOREIGN KEY ("category_id") REFERENCES "public"."categories"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "source_categories" ADD CONSTRAINT "source_categories_added_by_users_id_fk" FOREIGN KEY ("added_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sources" ADD CONSTRAINT "sources_verified_by_users_id_fk" FOREIGN KEY ("verified_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "system_settings" ADD CONSTRAINT "system_settings_updated_by_users_id_fk" FOREIGN KEY ("updated_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "webhooks" ADD CONSTRAINT "webhooks_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_analytics_events_type" ON "analytics_events" USING btree ("event_type");--> statement-breakpoint
CREATE INDEX "idx_analytics_events_timestamp" ON "analytics_events" USING btree ("timestamp");--> statement-breakpoint
CREATE INDEX "idx_analytics_events_source_id" ON "analytics_events" USING btree ("source_id");--> statement-breakpoint
CREATE INDEX "idx_analytics_events_api_key_id" ON "analytics_events" USING btree ("api_key_id");--> statement-breakpoint
CREATE INDEX "idx_analytics_events_ip" ON "analytics_events" USING btree ("ip_address");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_api_keys_hash" ON "api_keys" USING btree ("key_hash");--> statement-breakpoint
CREATE INDEX "idx_api_keys_owner" ON "api_keys" USING btree ("owner_email");--> statement-breakpoint
CREATE INDEX "idx_audit_logs_user_id" ON "audit_logs" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "idx_audit_logs_created_at" ON "audit_logs" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "idx_audit_logs_action" ON "audit_logs" USING btree ("action");--> statement-breakpoint
CREATE INDEX "idx_audit_logs_resource_type" ON "audit_logs" USING btree ("resource_type");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_categories_slug" ON "categories" USING btree ("slug");--> statement-breakpoint
CREATE INDEX "idx_categories_priority" ON "categories" USING btree ("priority");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_email_templates_name" ON "email_templates" USING btree ("name");--> statement-breakpoint
CREATE INDEX "idx_email_templates_is_active" ON "email_templates" USING btree ("is_active");--> statement-breakpoint
CREATE INDEX "idx_notifications_user_id" ON "notifications" USING btree ("user_id");--> statement-breakpoint
CREATE INDEX "idx_notifications_is_read" ON "notifications" USING btree ("is_read");--> statement-breakpoint
CREATE INDEX "idx_notifications_created_at" ON "notifications" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "idx_notifications_type" ON "notifications" USING btree ("type");--> statement-breakpoint
CREATE INDEX "idx_reports_status" ON "reports" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_reports_source_domain" ON "reports" USING btree ("source_domain");--> statement-breakpoint
CREATE INDEX "idx_reports_priority" ON "reports" USING btree ("priority");--> statement-breakpoint
CREATE INDEX "idx_reports_created_at" ON "reports" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "idx_reports_assigned_to" ON "reports" USING btree ("assigned_to");--> statement-breakpoint
CREATE INDEX "idx_scheduled_jobs_name" ON "scheduled_jobs" USING btree ("name");--> statement-breakpoint
CREATE INDEX "idx_scheduled_jobs_type" ON "scheduled_jobs" USING btree ("type");--> statement-breakpoint
CREATE INDEX "idx_scheduled_jobs_next_run" ON "scheduled_jobs" USING btree ("next_run");--> statement-breakpoint
CREATE INDEX "idx_scheduled_jobs_is_active" ON "scheduled_jobs" USING btree ("is_active");--> statement-breakpoint
CREATE INDEX "idx_source_analytics_source_id" ON "source_analytics" USING btree ("source_id");--> statement-breakpoint
CREATE INDEX "idx_source_analytics_date" ON "source_analytics" USING btree ("date");--> statement-breakpoint
CREATE UNIQUE INDEX "unique_source_date" ON "source_analytics" USING btree ("source_id","date");--> statement-breakpoint
CREATE INDEX "idx_source_categories_source_id" ON "source_categories" USING btree ("source_id");--> statement-breakpoint
CREATE INDEX "idx_source_categories_category_id" ON "source_categories" USING btree ("category_id");--> statement-breakpoint
CREATE UNIQUE INDEX "unique_source_category" ON "source_categories" USING btree ("source_id","category_id");--> statement-breakpoint
CREATE INDEX "idx_sources_domain" ON "sources" USING btree ("domain");--> statement-breakpoint
CREATE INDEX "idx_sources_status" ON "sources" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_sources_risk_level" ON "sources" USING btree ("risk_level");--> statement-breakpoint
CREATE INDEX "idx_sources_type" ON "sources" USING btree ("type");--> statement-breakpoint
CREATE INDEX "idx_sources_created_at" ON "sources" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "idx_sources_verified_by" ON "sources" USING btree ("verified_by");--> statement-breakpoint
CREATE INDEX "idx_sources_status_risk" ON "sources" USING btree ("status","risk_level");--> statement-breakpoint
CREATE INDEX "idx_system_metrics_type" ON "system_metrics" USING btree ("metric_type");--> statement-breakpoint
CREATE INDEX "idx_system_metrics_timestamp" ON "system_metrics" USING btree ("timestamp");--> statement-breakpoint
CREATE INDEX "idx_system_metrics_type_timestamp" ON "system_metrics" USING btree ("metric_type","timestamp");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_system_settings_key" ON "system_settings" USING btree ("key");--> statement-breakpoint
CREATE INDEX "idx_system_settings_category" ON "system_settings" USING btree ("category");--> statement-breakpoint
CREATE INDEX "idx_webhooks_created_by" ON "webhooks" USING btree ("created_by");--> statement-breakpoint
CREATE INDEX "idx_webhooks_is_active" ON "webhooks" USING btree ("is_active");