-- Meshkee CMS — expert product reviews

CREATE TABLE expert_reviews (
  id              BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  business_id     BIGINT NOT NULL,
  product_id      BIGINT NOT NULL,
  author_name     VARCHAR(255) NOT NULL,
  author_email    VARCHAR(255),
  rate            SMALLINT NOT NULL,
  positive_points TEXT[] NOT NULL DEFAULT '{}',
  negative_points TEXT[] NOT NULL DEFAULT '{}',
  text            TEXT NOT NULL,
  is_approved     BOOLEAN NOT NULL DEFAULT FALSE,
  approved_at     TIMESTAMPTZ,
  approved_by     BIGINT,
  created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),

  CONSTRAINT expert_reviews_business_id_fkey
    FOREIGN KEY (business_id) REFERENCES businesses (id) ON DELETE CASCADE,
  CONSTRAINT expert_reviews_product_id_fkey
    FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE,
  CONSTRAINT expert_reviews_approved_by_fkey
    FOREIGN KEY (approved_by) REFERENCES users (id) ON DELETE SET NULL,
  CONSTRAINT expert_reviews_rate_range CHECK (rate >= 1 AND rate <= 10),
  CONSTRAINT expert_reviews_text_nonempty CHECK (char_length(trim(text)) > 0),
  CONSTRAINT expert_reviews_author_name_nonempty CHECK (char_length(trim(author_name)) > 0)
);

CREATE INDEX idx_expert_reviews_business_approval
  ON expert_reviews (business_id, is_approved, created_at DESC);

CREATE INDEX idx_expert_reviews_product
  ON expert_reviews (business_id, product_id, is_approved, created_at DESC);

CREATE TRIGGER expert_reviews_set_updated_at
  BEFORE UPDATE ON expert_reviews
  FOR EACH ROW
  EXECUTE FUNCTION set_updated_at();

-- ---------------------------------------------------------------------------
-- permissions
-- ---------------------------------------------------------------------------
INSERT INTO permissions (name, slug, group_name, description) VALUES
  ('View expert reviews',    'expert_reviews.read',    'expert_reviews', 'View expert product reviews'),
  ('Approve expert reviews', 'expert_reviews.approve', 'expert_reviews', 'Approve or reject expert reviews'),
  ('Delete expert reviews',  'expert_reviews.delete',  'expert_reviews', 'Delete expert reviews')
ON CONFLICT (slug) DO NOTHING;

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.slug LIKE 'expert_reviews.%'
WHERE r.slug = 'business_owner'
ON CONFLICT DO NOTHING;

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.slug LIKE 'expert_reviews.%'
WHERE r.slug = 'owner'
ON CONFLICT DO NOTHING;

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.slug IN ('expert_reviews.read', 'expert_reviews.approve', 'expert_reviews.delete')
WHERE r.slug = 'admin'
ON CONFLICT DO NOTHING;

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.slug IN ('expert_reviews.read', 'expert_reviews.approve')
WHERE r.slug = 'editor'
ON CONFLICT DO NOTHING;

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.slug = 'expert_reviews.read'
WHERE r.slug = 'viewer'
ON CONFLICT DO NOTHING;
