-- Shipping cost groups (هزینه ارسال) + per-city overrides + category assignment CREATE TABLE IF NOT EXISTS shipping_cost_groups ( id BIGSERIAL PRIMARY KEY, business_id BIGINT NOT NULL REFERENCES businesses (id) ON DELETE CASCADE ON UPDATE NO ACTION, name VARCHAR(255) NOT NULL, name_en VARCHAR(255) NOT NULL, description TEXT NULL, methods TEXT[] NOT NULL DEFAULT '{}', default_amount NUMERIC(12, 2) NOT NULL DEFAULT 0, sort_order INT NOT NULL DEFAULT 0, is_active BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE INDEX IF NOT EXISTS idx_shipping_cost_groups_business_id ON shipping_cost_groups (business_id); CREATE INDEX IF NOT EXISTS idx_shipping_cost_groups_business_sort_order ON shipping_cost_groups (business_id, sort_order); DROP TRIGGER IF EXISTS shipping_cost_groups_set_updated_at ON shipping_cost_groups; CREATE TRIGGER shipping_cost_groups_set_updated_at BEFORE UPDATE ON shipping_cost_groups FOR EACH ROW EXECUTE FUNCTION set_updated_at(); CREATE TABLE IF NOT EXISTS shipping_cost_group_city_rates ( id BIGSERIAL PRIMARY KEY, shipping_cost_group_id BIGINT NOT NULL REFERENCES shipping_cost_groups (id) ON DELETE CASCADE ON UPDATE NO ACTION, city_id BIGINT NOT NULL REFERENCES cities (id) ON DELETE CASCADE ON UPDATE NO ACTION, amount NUMERIC(12, 2) NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT shipping_cost_group_city_rates_unique UNIQUE (shipping_cost_group_id, city_id) ); CREATE INDEX IF NOT EXISTS idx_shipping_cost_group_city_rates_group_id ON shipping_cost_group_city_rates (shipping_cost_group_id); CREATE INDEX IF NOT EXISTS idx_shipping_cost_group_city_rates_city_id ON shipping_cost_group_city_rates (city_id); DROP TRIGGER IF EXISTS shipping_cost_group_city_rates_set_updated_at ON shipping_cost_group_city_rates; CREATE TRIGGER shipping_cost_group_city_rates_set_updated_at BEFORE UPDATE ON shipping_cost_group_city_rates FOR EACH ROW EXECUTE FUNCTION set_updated_at(); CREATE TABLE IF NOT EXISTS shipping_cost_group_categories ( shipping_cost_group_id BIGINT NOT NULL REFERENCES shipping_cost_groups (id) ON DELETE CASCADE ON UPDATE NO ACTION, category_id BIGINT NOT NULL REFERENCES categories (id) ON DELETE CASCADE ON UPDATE NO ACTION, PRIMARY KEY (shipping_cost_group_id, category_id) ); CREATE INDEX IF NOT EXISTS idx_shipping_cost_group_categories_category_id ON shipping_cost_group_categories (category_id);