""" Internal raw-SQL helpers for company_profile_service. Separated from the service class because the INSERT/UPDATE statements are ~70 lines each; keeping them here lets the service module stay readable. """ import json from typing import Any, Optional from sqlalchemy import text from sqlalchemy.orm import Session from compliance.schemas.company_profile import CompanyProfileRequest def build_upsert_params( tid: str, pid: Optional[str], profile: CompanyProfileRequest ) -> dict[str, Any]: return { "tid": tid, "pid": pid, "company_name": profile.company_name, "legal_form": profile.legal_form, "industry": profile.industry, "founded_year": profile.founded_year, "business_model": profile.business_model, "offerings": json.dumps(profile.offerings), "offering_urls": json.dumps(profile.offering_urls), "company_size": profile.company_size, "employee_count": profile.employee_count, "annual_revenue": profile.annual_revenue, "hq_country": profile.headquarters_country, "hq_country_other": profile.headquarters_country_other, "hq_street": profile.headquarters_street, "hq_zip": profile.headquarters_zip, "hq_city": profile.headquarters_city, "hq_state": profile.headquarters_state, "has_intl": profile.has_international_locations, "intl_countries": json.dumps(profile.international_countries), "target_markets": json.dumps(profile.target_markets), "jurisdiction": profile.primary_jurisdiction, "is_controller": profile.is_data_controller, "is_processor": profile.is_data_processor, "uses_ai": profile.uses_ai, "ai_use_cases": json.dumps(profile.ai_use_cases), "dpo_name": profile.dpo_name, "dpo_email": profile.dpo_email, "legal_name": profile.legal_contact_name, "legal_email": profile.legal_contact_email, "machine_builder": json.dumps(profile.machine_builder) if profile.machine_builder else None, "is_complete": profile.is_complete, "repos": json.dumps(profile.repos), "document_sources": json.dumps(profile.document_sources), "processing_systems": json.dumps(profile.processing_systems), "ai_systems": json.dumps(profile.ai_systems), "technical_contacts": json.dumps(profile.technical_contacts), "subject_to_nis2": profile.subject_to_nis2, "subject_to_ai_act": profile.subject_to_ai_act, "subject_to_iso27001": profile.subject_to_iso27001, "supervisory_authority": profile.supervisory_authority, "review_cycle_months": profile.review_cycle_months, } def execute_update( db: Session, params: dict[str, Any], completed_at_sql: str, where_clause: str, ) -> None: db.execute( text(f"""UPDATE compliance_company_profiles SET company_name = :company_name, legal_form = :legal_form, industry = :industry, founded_year = :founded_year, business_model = :business_model, offerings = :offerings::jsonb, offering_urls = :offering_urls::jsonb, company_size = :company_size, employee_count = :employee_count, annual_revenue = :annual_revenue, headquarters_country = :hq_country, headquarters_country_other = :hq_country_other, headquarters_street = :hq_street, headquarters_zip = :hq_zip, headquarters_city = :hq_city, headquarters_state = :hq_state, has_international_locations = :has_intl, international_countries = :intl_countries::jsonb, target_markets = :target_markets::jsonb, primary_jurisdiction = :jurisdiction, is_data_controller = :is_controller, is_data_processor = :is_processor, uses_ai = :uses_ai, ai_use_cases = :ai_use_cases::jsonb, dpo_name = :dpo_name, dpo_email = :dpo_email, legal_contact_name = :legal_name, legal_contact_email = :legal_email, machine_builder = :machine_builder::jsonb, is_complete = :is_complete, repos = :repos::jsonb, document_sources = :document_sources::jsonb, processing_systems = :processing_systems::jsonb, ai_systems = :ai_systems::jsonb, technical_contacts = :technical_contacts::jsonb, subject_to_nis2 = :subject_to_nis2, subject_to_ai_act = :subject_to_ai_act, subject_to_iso27001 = :subject_to_iso27001, supervisory_authority = :supervisory_authority, review_cycle_months = :review_cycle_months, updated_at = NOW(), completed_at = {completed_at_sql} WHERE {where_clause}"""), params, ) def execute_insert( db: Session, params: dict[str, Any], completed_at_sql: str, ) -> None: db.execute( text(f"""INSERT INTO compliance_company_profiles (tenant_id, project_id, company_name, legal_form, industry, founded_year, business_model, offerings, offering_urls, company_size, employee_count, annual_revenue, headquarters_country, headquarters_country_other, headquarters_street, headquarters_zip, headquarters_city, headquarters_state, has_international_locations, international_countries, target_markets, primary_jurisdiction, is_data_controller, is_data_processor, uses_ai, ai_use_cases, dpo_name, dpo_email, legal_contact_name, legal_contact_email, machine_builder, is_complete, completed_at, repos, document_sources, processing_systems, ai_systems, technical_contacts, subject_to_nis2, subject_to_ai_act, subject_to_iso27001, supervisory_authority, review_cycle_months) VALUES (:tid, :pid, :company_name, :legal_form, :industry, :founded_year, :business_model, :offerings::jsonb, :offering_urls::jsonb, :company_size, :employee_count, :annual_revenue, :hq_country, :hq_country_other, :hq_street, :hq_zip, :hq_city, :hq_state, :has_intl, :intl_countries::jsonb, :target_markets::jsonb, :jurisdiction, :is_controller, :is_processor, :uses_ai, :ai_use_cases::jsonb, :dpo_name, :dpo_email, :legal_name, :legal_email, :machine_builder::jsonb, :is_complete, {completed_at_sql}, :repos::jsonb, :document_sources::jsonb, :processing_systems::jsonb, :ai_systems::jsonb, :technical_contacts::jsonb, :subject_to_nis2, :subject_to_ai_act, :subject_to_iso27001, :supervisory_authority, :review_cycle_months)"""), params, )