-- v1.28: Company Profile (for unified printing header/footer)
-- Run once on your DB.

CREATE TABLE IF NOT EXISTS company_profile (
  id TINYINT UNSIGNED NOT NULL,
  name VARCHAR(160) NOT NULL DEFAULT '',
  subtitle VARCHAR(255) NOT NULL DEFAULT '',
  address VARCHAR(255) NOT NULL DEFAULT '',
  phone VARCHAR(120) NOT NULL DEFAULT '',
  email VARCHAR(160) NOT NULL DEFAULT '',
  website VARCHAR(160) NOT NULL DEFAULT '',
  tax_no VARCHAR(80) NOT NULL DEFAULT '',
  logo_path VARCHAR(255) NOT NULL DEFAULT '',
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO company_profile (id, name, subtitle, address, phone, email, website, tax_no, logo_path)
VALUES (1, '', '', '', '', '', '', '', '')
ON DUPLICATE KEY UPDATE id=id;

-- Optional: register permission (if RBAC is enabled)
INSERT IGNORE INTO permissions (code, name, module)
VALUES ('admin.company.edit', 'تعديل تعريف الشركة', 'admin');

-- Try to grant it to common admin roles
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.code = 'admin.company.edit'
WHERE r.is_system = 1 OR r.name IN ('Administrator','Admin','المدير','مدير');
