
    `j	                        d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ dd	Zdd
ZddZy)u   Resolve structure capacity data for Ross1000 struttura blocks.

Reuses the same capacity calculation logic as C59 (available rooms/beds)
without importing from the C59 module — keeps the two pipelines independent.
    )annotations)ExpressionWrapperFIntegerFieldSum)Coalesce)Ross1000StructureError)Ross1000StrutturaPayload)Property)	Structurec                    t        | dd      xs t        | dd      }|rt        |      dkD  rt        |      S t        j                  j	                  | t        j
                  j                        j                         S )z3Count available (sellable) rooms for the structure.total_unitsNtotal_roomsr   	structureavailability)getattrintr   objectsfilterAvailability	AVAILABLEcount)r   totals     6/backend/istat/ross1000/services/structure_resolver.py_available_roomsr      so    I}d3^wy-Y]7^EUa5z""**44 #  eg    c           
     n   t         j                  j                  | t         j                  j                        j                  t        t        t        d      d      t        t        d      d      z   t                           j                  t        t        d      d            }t        |d	         S )
zDSum beds across all available properties (num_beds + num_sofa_beds).r   property_type__num_bedsr   property_type__num_sofa_beds)output_field)beds_per_roomr"   )r   r   )r   r   r   r   r   annotater   r   r   r   	aggregater   r   )r   results     r   _available_bedsr&      s     	!..88 	  	
 
+45q91;<a@A)^ 
 

 
#o"6:	;  vgr   c           	         t        t        | dd      xs d      j                         }|st        d| j                   d      t        |ddt        |       t        |             S )u   Build the base struttura payload for a structure.

    The camere_occupate field is set to 0 here — the movement builder
    overrides it per-day with the actual occupied count.

    Raises:
        Ross1000StructureError: If istat_code is missing.
    
istat_code z
Structure uC    is missing istat_code — cannot generate Ross1000 struttura block   r   )codiceaperturacamere_occupatecamere_disponibililetti_disponibili)strr   stripr	   idr
   r   r&   )r   r(   s     r   build_struttura_payloadr3   0   sr     WYb9?R@FFHJ$ '7 7
 	

 $+I6))4 r   N)r   r   returnr   )r   r   r4   r
   )__doc__
__future__r   django.db.modelsr   r   r   r   django.db.models.functionsr   istat.ross1000.exceptionsr	   &istat.ross1000.models.movement_payloadr
   properties.modelsr   structures.modelsr   r   r&   r3    r   r   <module>r>      s3    # D D / < K & ' &r   