create schema if not exists gis;

CREATE TABLE if not exists gis.rasters
(
  raster_id text NOT NULL DEFAULT next_id(),
  name text NOT NULL, -- Начва растру
  raster_key text, -- Унікальний ключ
  description text, -- Опис
  source_type text DEFAULT 'file'::text, -- Тип джерела, де зберігається растр
  source_path text NOT NULL, -- Шлях до растру
  geom geometry(Polygon,4326),
  resolution_x numeric, -- Координати x центру
  resolution_y numeric, -- Координати y центру
  format text, -- Форммат (розширення) растру
  raster_date date, -- Дата створення растру
  config jsonb, -- Опціональна конфігурація
  updated_at timestamp without time zone,
  created_at timestamp without time zone DEFAULT now(),
  created_by text,
  updated_by text,
  proj4 text, -- proj4
  raster_zoom integer DEFAULT 15, -- Зум для відображення оригіналу
  srid text DEFAULT 4326, -- Просторова система координат
  is_active boolean NOT NULL DEFAULT true,
  is_public boolean NOT NULL DEFAULT false,
  center geometry(Point,4326), -- Центр покриття
  bbox geometry(Polygon,4326), -- Область покриття сервісу
  group_id text, -- id групи
  CONSTRAINT rasters_pkey PRIMARY KEY (raster_id),
  CONSTRAINT rasters_raster_key_key UNIQUE (raster_key)
);

COMMENT ON TABLE gis.rasters IS 'Растри';

DROP INDEX if exists gis.gis_rasters_description_gin_idx;

CREATE INDEX gis_rasters_description_gin_idx
  ON gis.rasters
  USING gin
  (description COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_description_gin_idx_lower;

CREATE INDEX gis_rasters_description_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(description) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_format_gin_idx;

CREATE INDEX gis_rasters_format_gin_idx
  ON gis.rasters
  USING gin
  (format COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_format_gin_idx_lower;

CREATE INDEX gis_rasters_format_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(format) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_geom_4326_idx;

CREATE INDEX gis_rasters_geom_4326_idx
  ON gis.rasters
  USING gist
  (st_transform(geom, 4326));

DROP INDEX if exists gis.gis_rasters_geom_area_idx;

CREATE INDEX gis_rasters_geom_area_idx
  ON gis.rasters
  USING btree
  (st_area(st_transform(geom, 3857)));

DROP INDEX if exists gis.gis_rasters_geom_area_true_idx;

CREATE INDEX gis_rasters_geom_area_true_idx
  ON gis.rasters
  USING btree
  (st_area(geom::geography, true));

DROP INDEX if exists gis.gis_rasters_geom_gist1_idx;

CREATE INDEX gis_rasters_geom_gist1_idx
  ON gis.rasters
  USING gist
  (geom);

DROP INDEX if exists gis.gis_rasters_name_gin_idx;

CREATE INDEX gis_rasters_name_gin_idx
  ON gis.rasters
  USING gin
  (name COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_name_gin_idx_lower;

CREATE INDEX gis_rasters_name_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(name) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_raster_date_btree_idx;

CREATE INDEX gis_rasters_raster_date_btree_idx
  ON gis.rasters
  USING btree
  (raster_date);

DROP INDEX if exists gis.gis_rasters_raster_id_gin_idx;

CREATE INDEX gis_rasters_raster_id_gin_idx
  ON gis.rasters
  USING gin
  (raster_id COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_raster_id_gin_idx_lower;

CREATE INDEX gis_rasters_raster_id_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(raster_id) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_raster_key_gin_idx;

CREATE INDEX gis_rasters_raster_key_gin_idx
  ON gis.rasters
  USING gin
  (raster_key COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_raster_key_gin_idx_lower;

CREATE INDEX gis_rasters_raster_key_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(raster_key) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_resolution_x_btree_idx;

CREATE INDEX gis_rasters_resolution_x_btree_idx
  ON gis.rasters
  USING btree
  (resolution_x);

DROP INDEX if exists gis.gis_rasters_resolution_y_btree_idx;

CREATE INDEX gis_rasters_resolution_y_btree_idx
  ON gis.rasters
  USING btree
  (resolution_y);

DROP INDEX if exists gis.gis_rasters_source_path_gin_idx;

CREATE INDEX gis_rasters_source_path_gin_idx
  ON gis.rasters
  USING gin
  (source_path COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_source_path_gin_idx_lower;

CREATE INDEX gis_rasters_source_path_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(source_path) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_source_type_gin_idx;

CREATE INDEX gis_rasters_source_type_gin_idx
  ON gis.rasters
  USING gin
  (source_type COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_source_type_gin_idx_lower;

CREATE INDEX gis_rasters_source_type_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(source_type) COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_srid_btree_idx;

CREATE INDEX gis_rasters_srid_btree_idx
  ON gis.rasters
  USING btree
  (srid COLLATE pg_catalog."default");

DROP INDEX if exists gis.gis_rasters_srid_gin_idx;

CREATE INDEX gis_rasters_srid_gin_idx
  ON gis.rasters
  USING gin
  (srid COLLATE pg_catalog."default" gin_trgm_ops);

DROP INDEX if exists gis.gis_rasters_srid_gin_idx_lower;

CREATE INDEX gis_rasters_srid_gin_idx_lower
  ON gis.rasters
  USING gin
  (lower(srid) COLLATE pg_catalog."default" gin_trgm_ops);

-- Add columns if not exists
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS raster_id text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS name text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS raster_key text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS description text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS source_type text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS source_path text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS geom geometry(Polygon,4326);
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS resolution_x numeric;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS resolution_y numeric;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS format text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS raster_date date;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS config jsonb;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS updated_at timestamp without time zone;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS created_at timestamp without time zone;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS created_by text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS updated_by text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS proj4 text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS raster_zoom integer;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS srid text;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS is_active boolean;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS is_public boolean;
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS center geometry(Point,4326);
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS bbox geometry(Polygon,4326);
ALTER TABLE gis.rasters ADD COLUMN IF NOT EXISTS group_id text;

-- Set default values
ALTER TABLE gis.rasters ALTER COLUMN raster_id SET DEFAULT next_id();
ALTER TABLE gis.rasters ALTER COLUMN source_type SET DEFAULT 'file';
ALTER TABLE gis.rasters ALTER COLUMN created_at SET DEFAULT now();
ALTER TABLE gis.rasters ALTER COLUMN raster_zoom SET DEFAULT 15;
ALTER TABLE gis.rasters ALTER COLUMN srid SET DEFAULT '4326';
ALTER TABLE gis.rasters ALTER COLUMN is_active SET DEFAULT true;
ALTER TABLE gis.rasters ALTER COLUMN is_public SET DEFAULT false;

-- Set NOT NULL constraints
ALTER TABLE gis.rasters ALTER COLUMN raster_id SET NOT NULL;
update gis.rasters set name = '' where name is null;
ALTER TABLE gis.rasters ALTER COLUMN name SET NOT NULL;
update gis.rasters set source_path = '' where source_path is null;
ALTER TABLE gis.rasters ALTER COLUMN source_path SET NOT NULL;
update gis.rasters set is_active = true where is_active is null;
ALTER TABLE gis.rasters ALTER COLUMN is_active SET NOT NULL;
update gis.rasters set is_public = false where is_public is null;
ALTER TABLE gis.rasters ALTER COLUMN is_public SET NOT NULL;

COMMENT ON COLUMN gis.rasters.name IS 'Начва растру';
COMMENT ON COLUMN gis.rasters.raster_key IS 'Унікальний ключ';
COMMENT ON COLUMN gis.rasters.description IS 'Опис';
COMMENT ON COLUMN gis.rasters.source_type IS 'Тип джерела, де зберігається растр';
COMMENT ON COLUMN gis.rasters.source_path IS 'Шлях до растру';
COMMENT ON COLUMN gis.rasters.resolution_x IS 'Координати x центру';
COMMENT ON COLUMN gis.rasters.resolution_y IS 'Координати y центру';
COMMENT ON COLUMN gis.rasters.format IS 'Форммат (розширення) растру';
COMMENT ON COLUMN gis.rasters.raster_date IS 'Дата створення растру';
COMMENT ON COLUMN gis.rasters.config IS 'Опціональна конфігурація';
COMMENT ON COLUMN gis.rasters.proj4 IS 'proj4';
COMMENT ON COLUMN gis.rasters.raster_zoom IS 'Зум для відображення оригіналу';
COMMENT ON COLUMN gis.rasters.srid IS 'Просторова система координат';
COMMENT ON COLUMN gis.rasters.center IS 'Центр покриття';
COMMENT ON COLUMN gis.rasters.bbox IS 'Область покриття сервісу';
COMMENT ON COLUMN gis.rasters.group_id IS 'id групи';