-- +goose Up

CREATE TABLE IF NOT EXISTS "public"."language" ( 
  "id" SERIAL,
  "created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  "updated_at" TIMESTAMP WITH TIME ZONE NULL,
  "deleted_at" TIMESTAMP WITH TIME ZONE NULL,
  "name" VARCHAR(128) NOT NULL,
  "iso_code" VARCHAR(2) NOT NULL,
  "lang_code" VARCHAR(5) NOT NULL,
  "date_format" VARCHAR(32) NOT NULL,
  "date_format_short" VARCHAR(32) NOT NULL,
  "rtl" BOOLEAN NOT NULL DEFAULT false ,
  "is_default" BOOLEAN NOT NULL DEFAULT false ,
  "active" BOOLEAN NOT NULL DEFAULT true ,
  "flag" VARCHAR(16) NOT NULL,
  CONSTRAINT "language_pkey" PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "idx_language_deleted_at" 
ON "public"."language" (
  "deleted_at" ASC
);

INSERT INTO "public"."language" ("id", "created_at", "updated_at", "deleted_at", "name", "iso_code", "lang_code", "date_format", "date_format_short", "rtl", "is_default", "active", "flag") VALUES (1, '2022-09-16 17:10:02.837+00', '2026-03-02 21:24:36.77973+00', NULL, 'Polski', 'pl', 'pl', '__-__-____', '__-__', false, false, true, '🇵🇱') ON CONFLICT DO NOTHING;
INSERT INTO "public"."language" ("id", "created_at", "updated_at", "deleted_at", "name", "iso_code", "lang_code", "date_format", "date_format_short", "rtl", "is_default", "active", "flag") VALUES (2, '2022-09-16 17:10:02.852+00', '2026-03-02 21:24:36.77973+00', NULL, 'English', 'en', 'en', '__-__-____', '__-__', false, true, true, '🇬🇧') ON CONFLICT DO NOTHING;
INSERT INTO "public"."language" ("id", "created_at", "updated_at", "deleted_at", "name", "iso_code", "lang_code", "date_format", "date_format_short", "rtl", "is_default", "active", "flag") VALUES (3, '2022-09-16 17:10:02.865+00', '2026-03-02 21:24:36.77973+00', NULL, 'Čeština', 'cs', 'cs', '__-__-____', '__-__', false, false, true, '🇨🇿') ON CONFLICT DO NOTHING;


-- components
CREATE TABLE IF NOT EXISTS "public"."components" ( 
  "id" SERIAL,
  "name" VARCHAR(255) NOT NULL,
  CONSTRAINT "components_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX IF NOT EXISTS "uk_components_name" 
ON "public"."components" (
  "name" ASC,
  "id" ASC
);

-- scopes
CREATE TABLE IF NOT EXISTS "public"."scopes" ( 
  "id" SERIAL,
  "name" VARCHAR(255) NOT NULL,
  CONSTRAINT "scopes_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX IF NOT EXISTS "uk_scopes_name" 
ON "public"."scopes" (
  "name" ASC
);

-- translations
CREATE TABLE IF NOT EXISTS "public"."translations" ( 
  "lang_id" BIGINT NOT NULL,
  "scope_id" BIGINT NOT NULL,
  "component_id" BIGINT NOT NULL,
  "key" VARCHAR(255) NOT NULL,
  "data" TEXT NULL,
  CONSTRAINT "translations_pkey" PRIMARY KEY ("lang_id", "scope_id", "component_id", "key"),
  CONSTRAINT "fk_translations_language" FOREIGN KEY ("lang_id") REFERENCES "public"."language" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT "fk_translations_scope" FOREIGN KEY ("scope_id") REFERENCES "public"."scopes" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT "fk_translations_component" FOREIGN KEY ("component_id") REFERENCES "public"."components" ("id") ON DELETE RESTRICT ON UPDATE RESTRICT
);



-- customers
CREATE TABLE IF NOT EXISTS "public"."customers" ( 
  "id" SERIAL,
  "email" VARCHAR(255) NOT NULL,
  "password" VARCHAR(255) NULL,
  "first_name" VARCHAR(100) NULL,
  "last_name" VARCHAR(100) NULL,
  "role" VARCHAR(20) NULL DEFAULT 'user'::character varying ,
  "provider" VARCHAR(20) NULL DEFAULT 'local'::character varying ,
  "provider_id" VARCHAR(255) NULL,
  "avatar_url" VARCHAR(500) NULL,
  "is_active" BOOLEAN NULL DEFAULT true ,
  "email_verified" BOOLEAN NULL DEFAULT false ,
  "email_verification_token" VARCHAR(255) NULL,
  "email_verification_expires" TIMESTAMP WITH TIME ZONE NULL,
  "password_reset_token" VARCHAR(255) NULL,
  "password_reset_expires" TIMESTAMP WITH TIME ZONE NULL,
  "last_password_reset_request" TIMESTAMP WITH TIME ZONE NULL,
  "last_login_at" TIMESTAMP WITH TIME ZONE NULL,
  "lang" VARCHAR(10) NULL DEFAULT 'en'::character varying ,
  "created_at" TIMESTAMP WITH TIME ZONE NULL,
  "updated_at" TIMESTAMP WITH TIME ZONE NULL,
  "deleted_at" TIMESTAMP WITH TIME ZONE NULL,
  CONSTRAINT "customers_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX IF NOT EXISTS "idx_customers_email" 
ON "public"."customers" (
  "email" ASC
);
CREATE INDEX IF NOT EXISTS "idx_customers_deleted_at" 
ON "public"."customers" (
  "deleted_at" ASC
);


-- customer_repo_accesses
CREATE TABLE IF NOT EXISTS "public"."customer_repo_accesses" (
  "user_id" BIGINT NOT NULL,
  "repo_id" BIGINT NOT NULL,
  PRIMARY KEY ("user_id", "repo_id"),
  FOREIGN KEY ("user_id") REFERENCES "public"."customers" ("id") ON DELETE CASCADE,
  FOREIGN KEY ("repo_id") REFERENCES "public"."repository" ("id") ON DELETE CASCADE
);


-- refresh_tokens
CREATE TABLE IF NOT EXISTS "public"."refresh_tokens" (
  "id" SERIAL,
  "customer_id" BIGINT NOT NULL,
  "token_hash" VARCHAR(64) NOT NULL,
  "expires_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  "created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
  CONSTRAINT "refresh_tokens_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "fk_refresh_tokens_customer" FOREIGN KEY ("customer_id") REFERENCES "public"."customers" ("id") ON DELETE CASCADE
);
CREATE UNIQUE INDEX IF NOT EXISTS "uk_refresh_tokens_token_hash"
ON "public"."refresh_tokens" (
  "token_hash" ASC
);
CREATE INDEX IF NOT EXISTS "idx_refresh_tokens_customer_id"
ON "public"."refresh_tokens" (
  "customer_id" ASC
);


-- insert sample admin user admin@ma-al.com/Maal12345678
INSERT INTO "public"."customers" ("id", "email", "password", "first_name", "last_name", "role", "provider", "provider_id", "avatar_url", "is_active", "email_verified", "email_verification_token", "email_verification_expires", "password_reset_token", "password_reset_expires", "last_password_reset_request", "last_login_at", "lang", "created_at", "updated_at", "deleted_at") VALUES (1, 'admin@ma-al.com', '$2a$10$Owy9DjrS0l3Fz4XoOvh5pulgmOMqdwXmb7hYE9BovnSuWS2plGr82', 'Super', 'Admin', 'admin', 'local', '', '', true, true, NULL, NULL, '', NULL, NULL, NULL, 'pl', '2026-03-02 16:55:10.25274+00', '2026-03-02 16:55:10.25274+00', NULL) ON CONFLICT DO NOTHING;


-- +goose Down

DROP TABLE IF EXISTS "public"."language";
DROP TABLE IF EXISTS "public"."components";
DROP TABLE IF EXISTS "public"."scopes";
DROP TABLE IF EXISTS "public"."translations";
DROP TABLE IF EXISTS "public"."customers";
DROP TABLE IF EXISTS "public"."refresh_tokens";
