-- t-paliad-086 PR-1: import youpc deadline-calc data into paliad.
--
-- youpc.org's public deadline calc is event-driven: pick a trigger event,
-- see all deadlines that flow from it. Paliad's existing Fristenrechner is
-- proceeding-tree-driven (paliad.deadline_rules, ported from KanzlAI which
-- itself came from youpc data.proceeding_events). This migration mirrors
-- youpc's *other* deadline tables — data.events + data.deadlines +
-- data.deadline_events + data.deadline_rule_codes — into paliad so PR-2
-- can build the "Was kommt nach…" UI mode against a local source of truth.
--
-- IDs are preserved verbatim from youpc (gaps included, e.g. 48/51/58)
-- so a future re-sync can diff against the source. Sequences start
-- past the seeded range so new rows don't collide.
--
-- Composite "31d OR 20 working_days, whichever is longer" rules
-- (R.198 and R.213 — start of merits after evidence preservation /
-- provisional measures) are encoded via alt_* + combine_op='max' so
-- PR-2's calculator can resolve them without a special case. The
-- working_days unit is a new primitive; PR-2 wires it into the calc.
--
-- One youpc data fix applied during import: rule_code 'Rop.109' (lowercase
-- 'op', deadline 69) → 'RoP.109'. Single typo in source; matches paliad
-- audit recommendation 4 (canonical RoP.NNN.x format).

-- ============================================================================
-- Schema
-- ============================================================================

CREATE TABLE paliad.trigger_events (
    id           bigint PRIMARY KEY,
    code         text NOT NULL UNIQUE,
    name         text NOT NULL,                  -- English (source-of-truth)
    name_de      text NOT NULL DEFAULT '',       -- bilingual; backfill in PR-2/follow-up
    description  text NOT NULL DEFAULT '',
    is_active    boolean NOT NULL DEFAULT true,
    created_at   timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX trigger_events_code_idx ON paliad.trigger_events (code);

CREATE TABLE paliad.event_deadlines (
    id                  bigint PRIMARY KEY,
    trigger_event_id    bigint NOT NULL REFERENCES paliad.trigger_events(id) ON DELETE CASCADE,
    title               text NOT NULL,           -- English
    title_de            text NOT NULL DEFAULT '',
    duration_value      integer NOT NULL DEFAULT 0,
    duration_unit       text NOT NULL DEFAULT 'days'
                        CHECK (duration_unit IN ('days', 'weeks', 'months', 'working_days')),
    timing              text NOT NULL DEFAULT 'after'
                        CHECK (timing IN ('before', 'after')),
    notes               text NOT NULL DEFAULT '',
    -- Composite-rule support: when alt_* + combine_op are non-null, the
    -- final due date = combine_op( base, alt ). Used for R.198/R.213
    -- ("31 days OR 20 working days, whichever is longer").
    alt_duration_value  integer,
    alt_duration_unit   text CHECK (alt_duration_unit IS NULL OR alt_duration_unit IN ('days', 'weeks', 'months', 'working_days')),
    combine_op          text CHECK (combine_op IS NULL OR combine_op IN ('max', 'min')),
    is_active           boolean NOT NULL DEFAULT true,
    created_at          timestamptz NOT NULL DEFAULT now(),
    updated_at          timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX event_deadlines_trigger_event_idx ON paliad.event_deadlines (trigger_event_id);
CREATE INDEX event_deadlines_active_idx        ON paliad.event_deadlines (is_active) WHERE is_active = true;

CREATE TABLE paliad.event_deadline_rule_codes (
    event_deadline_id  bigint NOT NULL REFERENCES paliad.event_deadlines(id) ON DELETE CASCADE,
    rule_code          text NOT NULL,
    sort_order         integer NOT NULL DEFAULT 0,
    PRIMARY KEY (event_deadline_id, rule_code)
);

CREATE INDEX event_deadline_rule_codes_code_idx ON paliad.event_deadline_rule_codes (rule_code);

-- ============================================================================
-- Seed: trigger_events (102 rows from youpc.data.events)
-- ============================================================================

INSERT INTO paliad.trigger_events (id, code, name, description) VALUES
    (1, 'statement_of_defence_which_includes_a_counterclaim_for_revocation', 'Statement of defence which includes a Counterclaim for Revocation', 'Trigger event: Statement of defence which includes a Counterclaim for Revocation'),
    (2, 'decision_on_fixation_of_costs_rule_157', 'Decision on fixation of costs (Rule 157)', 'Trigger event: Decision on fixation of costs (Rule 157)'),
    (3, 'opponent_submission', 'Opponent Submission', 'Trigger event: Opponent Submission'),
    (4, 'rejoinder_to_the_reply_to_the_defence_to_the_statement_for_a_declaration_of_non_infringement', 'Rejoinder to the Reply to the Defence to the Statement for a declaration of non-infringement', 'Required action: Rejoinder to the Reply to the Defence to the Statement for a declaration of non-infringement'),
    (5, 'statement_of_claim', 'Statement of Claim', ''),
    (6, 'statement_for_revocation', 'Statement for Revocation', 'Trigger event: Statement for Revocation'),
    (7, 'statement_for_a_declaration_of_non_infringement', 'Statement for a declaration of non-infringement', 'Trigger event: Statement for a declaration of non-infringement'),
    (8, 'application_for_cost_decision', 'Application for cost decision', 'Required action: Application for cost decision'),
    (9, 'reply_to_the_defence_to_revocation_defence_to_an_application_to_amend_the_patent_defence_to_the_counterclaim_of_infringement', 'Reply to the Defence to revocation, Defence to an Application to amend the patent, Defence to the Counterclaim of infringement', 'Required action: Reply to the Defence to revocation, Defence to an Application to amend the patent, Defence to the Counterclaim of infringement'),
    (10, 'counterclaim_for_infringement', 'Counterclaim for Infringement', 'Required action: Counterclaim for Infringement'),
    (11, 'statement_of_appeal_against_a_decision_referred_to_in_rule_2201a_and_b', 'Statement of Appeal against a decision referred to in Rule 220.1(a) and (b)', 'Required action: Statement of Appeal against a decision referred to in Rule 220.1(a) and (b)'),
    (12, 'rejoinder_to_the_reply_to_the_defence_to_an_application_to_amend_the_patent', 'Rejoinder to the Reply to the Defence to an Application to amend the patent', 'Required action: Rejoinder to the Reply to the Defence to an Application to amend the patent'),
    (13, 'request_for_discretionary_review', 'Request for discretionary review', 'Required action: Request for discretionary review'),
    (14, 'defence_to_the_application_for_the_determination_of_damages', 'Defence to the Application for the determination of damages', 'Trigger event: Defence to the Application for the determination of damages'),
    (15, 'challenge_of_a_decision_to_reject_the_appeal_as_inadmissible', 'Challenge of a decision to reject the appeal as inadmissible', 'Required action: Challenge of a decision to reject the appeal as inadmissible'),
    (16, 'application_for_the_review_of_a_case_management_order', 'Application for the review of a case management order', 'Required action: Application for the review of a case management order'),
    (17, 'organize_translation_issue', 'Organize Translation Issue', 'Required action: Organize Translation Issue'),
    (18, 'appeal_orders_&_with_leave', 'Appeal (Orders & with leave)', 'Required action: Appeal (Orders & with leave)'),
    (19, 'defence_to_the_application_to_amend_the_patent', 'Defence to the Application to amend the patent', 'Required action: Defence to the Application to amend the patent'),
    (20, 'reply_to_the_defence_to_an_application_to_amend_the_patent', 'Reply to the Defence to an Application to amend the patent', 'Trigger event: Reply to the Defence to an Application to amend the patent'),
    (21, 'reply_to_the_defence_to_revocation', 'Reply to the Defence to revocation', 'Trigger event: Reply to the Defence to revocation'),
    (22, 'reply_to_the_defence_to_the_statement_for_a_declaration_of_non_infringement', 'Reply to the Defence to the Statement for a declaration of non-infringement', 'Trigger event: Reply to the Defence to the Statement for a declaration of non-infringement'),
    (23, 'decision_to_reject_an_appeal_as_inadmissible', 'Decision to reject an appeal as inadmissible', 'Trigger event: Decision to reject an appeal as inadmissible'),
    (24, 'application_for_rehearing_fundamental_procedural_defect', 'Application for Rehearing (fundamental procedural defect)', 'Required action: Application for Rehearing (fundamental procedural defect)'),
    (25, 'application_to_request_confidentiality_from_the_public', 'Application to request confidentiality from the public', 'Required action: Application to request confidentiality from the public'),
    (26, 'date_specified_in_the_courts_order_to_preserve_evidence', 'Date specified in the Court''s order to preserve evidence', 'Trigger event: Date specified in the Court''s order to preserve evidence'),
    (27, 'lodging_of_translations_of_documents', 'Lodging of translations of documents', 'Required action: Lodging of translations of documents'),
    (28, 'statement_of_defence_without_a_counterclaim_for_revocation', 'Statement of defence without a Counterclaim for Revocation', 'Trigger event: Statement of defence without a Counterclaim for Revocation'),
    (29, 'rejoinder_to_the_reply_reply_to_the_defence_to_an_application_to_amend_the_patent', 'Rejoinder to the Reply, Reply to the Defence to an Application to amend the patent', 'Required action: Rejoinder to the Reply, Reply to the Defence to an Application to amend the patent'),
    (30, 'reply_to_the_defence_to_the_counterclaim_for_infringement', 'Reply to the Defence to the Counterclaim for infringement', 'Required action: Reply to the Defence to the Counterclaim for infringement'),
    (31, 'statement_of_response_to_the_appeal_pursuant_to_rule_2242a', 'Statement of response (to the Appeal pursuant to Rule 224.2(a))', 'Required action: Statement of response (to the Appeal pursuant to Rule 224.2(a))'),
    (32, 'information_on_engaging_an_interpreter_at_partys_expense', 'Information on engaging an interpreter at party''s expense', 'Required action: Information on engaging an interpreter at party''s expense'),
    (33, 'defence_to_an_application_to_amend_the_patent', 'Defence to an Application to amend the patent', 'Trigger event: Defence to an Application to amend the patent'),
    (34, 'defence_to_revocation', 'Defence to revocation', 'Required action: Defence to revocation'),
    (35, 'application_to_annul_or_alter_a_decision_of_the_office', 'Application to annul or alter a decision of the Office', 'Required action: Application to annul or alter a decision of the Office'),
    (36, 'application_for_orders_consequential_on_a_final_decision_on_validity', 'Application for orders consequential on a final decision on validity', 'Required action: Application for orders consequential on a final decision on validity'),
    (37, 'decision_referred_to_in_rule_2201a_and_b', 'Decision referred to in Rule 220.1(a) and (b)', 'Trigger event: Decision referred to in Rule 220.1(a) and (b)'),
    (38, 'application_to_amend_the_patent', 'Application to amend the patent', 'Trigger event: Application to amend the patent'),
    (39, 'decision_of_the_epo', 'Decision of the EPO', 'Trigger event: Decision of the EPO'),
    (40, 'reply_to_the_defence_to_the_request_to_lay_open_books', 'Reply to the Defence to the Request to lay open books', 'Trigger event: Reply to the Defence to the Request to lay open books'),
    (41, 'rectification_of_decisions_and_orders', 'Rectification of decisions and orders', 'Required action: Rectification of decisions and orders'),
    (42, 'defence_to_counterclaim', 'Defence to Counterclaim', 'Trigger event: Defence to Counterclaim'),
    (43, 'reply_to_the_defence_to_the_application_for_the_determination_of_damages', 'Reply to the Defence to the Application for the determination of damages', 'Required action: Reply to the Defence to the Application for the determination of damages'),
    (44, 'execution_of_measures_to_preserve_evidence', 'Execution of measures to preserve evidence', 'Trigger event: Execution of measures to preserve evidence'),
    (45, 'decision_of_the_epo_not_to_grant_unitary_effect', 'Decision of the EPO not to grant unitary effect', 'Trigger event: Decision of the EPO not to grant unitary effect'),
    (46, 'renewal_of_protective_letter', 'Renewal of Protective Letter', 'Required action: Renewal of Protective Letter'),
    (47, 'defence_to_a_counterclaim_for_infringement', 'Defence to a Counterclaim for infringement', 'Trigger event: Defence to a Counterclaim for infringement'),
    (49, 'oral_hearing', 'Oral hearing', 'Trigger event: Oral hearing'),
    (50, 'order_referred_to_in_rule_2201c_or_a_decision_referred_to_in_rule_2202_or_2213', 'Order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3', 'Trigger event: Order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3'),
    (52, 'final_decision_of_the_central_division_court_of_appeal_or_epo_on_the_validity_of_the_patent', 'Final decision of the central division, Court of Appeal or EPO on the validity of the patent', 'Trigger event: Final decision of the central division, Court of Appeal or EPO on the validity of the patent'),
    (53, 'reply_to_a_statement_of_cross_appeal_to_the_appeal_pursuant_to_rule_2242b', 'Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))', 'Required action: Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))'),
    (54, 'request_for_a_referral_to_the_central_division', 'Request for a referral to the central division', 'Required action: Request for a referral to the central division'),
    (55, 'protective_letter', 'Protective Letter', 'Trigger event: Protective Letter'),
    (56, 'summons_to_oral_hearing', 'Summons to Oral Hearing', 'Trigger event: Summons to Oral Hearing'),
    (57, 'reply_to_the_statement_of_defence_without_a_counterclaim_for_revocation', 'Reply to the Statement of defence without a counterclaim for revocation', ''),
    (60, 'statement_of_grounds_of_appeal_pursuant_to_rule_2242b', 'Statement of grounds of appeal pursuant to Rule 224.2(b)', 'Trigger event: Statement of grounds of appeal pursuant to Rule 224.2(b)'),
    (61, 'rejoinder_to_the_reply_to_the_statement_of_defence', 'Rejoinder to the Reply to the Statement of Defence', ''),
    (62, 'request_to_lay_open_books', 'Request to lay open books', 'Trigger event: Request to lay open books'),
    (63, 'statement_of_cross_appeal_to_the_appeal_pursuant_to_rule_2242a', 'Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))', 'Required action: Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))'),
    (64, 'statement_of_cross_appeal_to_the_appeal_pursuant_to_rule_2242b', 'Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))', 'Required action: Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))'),
    (65, 'request_for_review_of_the_order_to_preserve_evidence', 'Request for review of the order to preserve evidence', 'Required action: Request for review of the order to preserve evidence'),
    (66, 'date_specified_in_the_courts_order_for_provisional_measures', 'Date specified in the Court''s order for provisional measures', 'Trigger event: Date specified in the Court''s order for provisional measures'),
    (67, 'application_to_annul_a_decision_of_the_epo_to_reject_a_request_for_unitary_effect', 'Application to annul a decision of the EPO to reject a request for unitary effect', 'Required action: Application to annul a decision of the EPO to reject a request for unitary effect'),
    (68, 'preliminary_objection', 'Preliminary Objection', 'Preliminary Objection'),
    (70, 'reply_to_a_statement_of_cross_appeal_to_the_appeal_pursuant_to_rule_2242a', 'Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))', 'Required action: Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))'),
    (71, 'notification_by_the_registry_to_correct_deficiencies', 'Notification by the Registry to correct deficiencies', 'Trigger event: Notification by the Registry to correct deficiencies'),
    (73, 'statement_of_response_to_the_appeal_pursuant_to_rule_2242b', 'Statement of response (to the Appeal pursuant to Rule 224.2(b))', 'Required action: Statement of response (to the Appeal pursuant to Rule 224.2(b))'),
    (74, 'correction_of_deficiencies_payment', 'Correction of deficiencies / Payment', 'Required action: Correction of deficiencies / Payment'),
    (75, 'information_by_the_court_not_to_approve_application_to_use_the_patents_language_as_language_of_the_proceedings', 'Information by the Court not to approve Application to use the patent''s language as language of the proceedings', 'Trigger event: Information by the Court not to approve Application to use the patent''s language as language of the proceedings'),
    (76, 'defence_to_the_request_to_lay_open_books', 'Defence to the Request to lay open books', 'Trigger event: Defence to the Request to lay open books'),
    (77, 'statement_of_grounds_of_appeal_against_a_decision_referred_to_in_rule_2201a_and_b', 'Statement of grounds of appeal against a decision referred to in Rule 220.1(a) and (b)', 'Required action: Statement of grounds of appeal against a decision referred to in Rule 220.1(a) and (b)'),
    (78, 'case_management_order_service', 'Case management order (Service)', 'Trigger event: Case management order (Service)'),
    (80, 'correction_of_deficiencies_submission_of_written_comments', 'Correction of deficiencies / Submission of written comments', 'Required action: Correction of deficiencies / Submission of written comments'),
    (81, 'start_of_proceedings_on_the_merits', 'Start of proceedings on the merits', 'Required action: Start of proceedings on the merits'),
    (82, 'application_for_the_determination_of_damages_indication_pursuant_to_rule_1312', 'Application for the determination of damages / Indication pursuant to Rule 131.2', 'Trigger event: Application for the determination of damages / Indication pursuant to Rule 131.2'),
    (84, 'statement_of_defence', 'Statement of Defence', 'Required action: Statement of Defence'),
    (85, 'reply_to_the_statement_of_defence', 'Reply to the Statement of Defence', ''),
    (86, 'request_for_simultaneous_translation', 'Request for simultaneous translation', 'Required action: Request for simultaneous translation'),
    (87, 'statement_of_cross_appeal_under_rules_237_2351', 'Statement of cross-appeal under rules 237, 235.1', 'Trigger event: Statement of cross-appeal under rules 237, 235.1'),
    (88, 'final_decision_service_court_decision_on_criminal_offence_whichever_is_later', 'Final decision (Service) / Court decision on criminal offence (whichever is later)', 'Trigger event: Final decision (Service) / Court decision on criminal offence (whichever is later)'),
    (89, 'statement_of_grounds_of_appeal_pursuant_to_rule_2242a', 'Statement of grounds of appeal pursuant to Rule 224.2(a)', 'Trigger event: Statement of grounds of appeal pursuant to Rule 224.2(a)'),
    (90, 'statement_of_cross_appeal_under_rules_237_2352', 'Statement of cross-appeal under rules 237, 235.2', 'Trigger event: Statement of cross-appeal under rules 237, 235.2'),
    (92, 'rejoinder_to_the_reply_to_the_defence_to_the_counterclaim_for_infringement_rejoinder_to_the_reply_to_the_defence_to_the_application_to_amend_the_patent', 'Rejoinder to the Reply to the Defence to the Counterclaim for infringement, Rejoinder to the Reply to the Defence to the Application to amend the patent', 'Required action: Rejoinder to the Reply to the Defence to the Counterclaim for infringement, Rejoinder to the Reply to the Defence to the Application to amend the patent'),
    (93, 'defence_to_the_statement_for_a_declaration_of_non_infringement', 'Defence to the Statement for a declaration of non-infringement', 'Trigger event: Defence to the Statement for a declaration of non-infringement'),
    (95, 'statement_of_grounds_of_appeal_against_an_order_referred_to_in_rule_2201c_or_a_decision_referred_to_in_rule_2202_or_2213', 'Statement of grounds of appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3', 'Required action: Statement of grounds of appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3'),
    (96, 'application_for_rehearing_criminal_offence', 'Application for Rehearing (criminal offence)', 'Required action: Application for Rehearing (criminal offence)'),
    (97, 'reply_to_the_defence_to_the_counterclaim_rejoinder_to_the_reply_to_the_statement_of_defence_defence_to_an_application_to_amend_the_patent', 'Reply to the Defence to the Counterclaim, Rejoinder to the Reply to the Statement of Defence, Defence to an Application to amend the patent', 'Required action: Reply to the Defence to the Counterclaim, Rejoinder to the Reply to the Statement of Defence, Defence to an Application to amend the patent'),
    (98, 'final_decision_service_discovery_of_the_fundamental_defect_whichever_is_later', 'Final decision (Service) / Discovery of the fundamental defect (whichever is later)', 'Trigger event: Final decision (Service) / Discovery of the fundamental defect (whichever is later)'),
    (99, 'leave_to_appeal_refused_within_15_days_of_the_order', 'Leave to Appeal refused within 15 days of the order', 'Trigger event: Leave to Appeal refused within 15 days of the order'),
    (100, 'leave_to_appeal_granted', 'Leave to Appeal granted', 'Trigger event: Leave to Appeal granted'),
    (101, 'counterclaim_for_revocation', 'Counterclaim for Revocation', 'Required action: Counterclaim for Revocation'),
    (102, 'defence_to_the_counterclaim_for_revocation_and_reply_to_the_statement_of_defence', 'Defence to the Counterclaim for revocation and Reply to the Statement of defence', 'Required action: Defence to the Counterclaim for revocation and Reply to the Statement of defence'),
    (103, 'rejoinder_to_the_reply_to_the_defence_to_the_application_for_the_determination_of_damages', 'Rejoinder to the Reply to the Defence to the Application for the determination of damages', 'Required action: Rejoinder to the Reply to the Defence to the Application for the determination of damages'),
    (104, 'decision_on_the_merits', 'Decision on the merits', 'Trigger event: Decision on the merits'),
    (105, 'decision_or_order', 'Decision or order', 'Trigger event: Decision or order'),
    (106, 'reply_to_the_defence_to_the_counterclaim', 'Reply to the Defence to the Counterclaim', 'Trigger event: Reply to the Defence to the Counterclaim'),
    (108, 'rejoinder_to_the_reply_to_the_defence_to_the_request_to_lay_open_books', 'Rejoinder to the Reply to the Defence to the Request to lay open books', 'Required action: Rejoinder to the Reply to the Defence to the Request to lay open books'),
    (109, 'statement_of_appeal_against_an_order_referred_to_in_rule_2201c_or_a_decision_referred_to_in_rule_2202_or_2213', 'Statement of Appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3', 'Required action: Statement of Appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3'),
    (110, 'application_for_leave_to_appeal_against_cost_decisions', 'Application for leave to appeal against cost decisions', 'Required action: Application for leave to appeal against cost decisions'),
    (111, 'defence_to_the_counterclaim_for_infringement', 'Defence to the Counterclaim for infringement', 'Required action: Defence to the Counterclaim for infringement'),
    (113, 'order_of_the_judge_rapporteur_to_lodge_translations', 'Order of the judge-rapporteur to lodge translations', 'Trigger event: Order of the judge-rapporteur to lodge translations'),
    (114, 'rejoinder_to_the_reply_to_the_defence_to_revocation', 'Rejoinder to the Reply to the Defence to revocation', 'Required action: Rejoinder to the Reply to the Defence to revocation');

-- ============================================================================
-- Seed: event_deadlines (70 rows from youpc.data.deadlines)
-- ============================================================================

INSERT INTO paliad.event_deadlines (id, trigger_event_id, title, duration_value, duration_unit, timing, notes) VALUES
    (1, 5, 'Preliminary Objection', 1, 'months', 'after', ''),
    (2, 5, 'Statement of Defence', 3, 'months', 'after', ''),
    (3, 1, 'Defence to the Counterclaim for revocation and Reply to the Statement of defence', 2, 'months', 'after', ''),
    (4, 28, 'Reply to the Statement of Defence', 2, 'months', 'after', ''),
    (5, 57, 'Rejoinder to the Reply to the Statement of Defence', 1, 'months', 'after', ''),
    (6, 1, 'Application to amend the patent', 2, 'months', 'after', ''),
    (7, 42, 'Reply to the Defence to the Counterclaim, Rejoinder to the Reply to the Statement of Defence, Defence to an Application to amend the patent', 2, 'months', 'after', ''),
    (8, 106, 'Rejoinder to the Reply, Reply to the Defence to an Application to amend the patent', 1, 'months', 'after', ''),
    (9, 38, 'Defence to the Application to amend the patent', 2, 'months', 'after', ''),
    (10, 33, 'Reply to the Defence to an Application to amend the patent', 1, 'months', 'after', ''),
    (11, 20, 'Rejoinder to the Reply to the Defence to an Application to amend the patent', 1, 'months', 'after', ''),
    (12, 113, 'Lodging of translations of documents', 1, 'months', 'after', ''),
    (13, 6, 'Defence to revocation', 2, 'months', 'after', ''),
    (14, 34, 'Reply to the Defence to revocation, Defence to an Application to amend the patent, Defence to the Counterclaim of infringement', 2, 'months', 'after', ''),
    (15, 21, 'Rejoinder to the Reply to the Defence to revocation', 1, 'months', 'after', ''),
    (16, 10, 'Defence to the Counterclaim for infringement', 2, 'months', 'after', ''),
    (17, 47, 'Reply to the Defence to the Counterclaim for infringement', 1, 'months', 'after', ''),
    (18, 30, 'Rejoinder to the Reply to the Defence to the Counterclaim for infringement, Rejoinder to the Reply to the Defence to the Application to amend the patent', 1, 'months', 'after', ''),
    (19, 7, 'Defence to the Statement for a declaration of non-infringement', 2, 'months', 'after', ''),
    (20, 93, 'Reply to the Defence to the Statement for a declaration of non-infringement', 1, 'months', 'after', ''),
    (21, 22, 'Rejoinder to the Reply to the Defence to the Statement for a declaration of non-infringement', 1, 'months', 'after', ''),
    (22, 39, 'Application to annul or alter a decision of the Office', 1, 'months', 'after', ''),
    (23, 49, 'Request for simultaneous translation', 1, 'months', 'before', ''),
    (24, 52, 'Application for orders consequential on a final decision on validity', 2, 'months', 'after', ''),
    (25, 82, 'Defence to the Application for the determination of damages', 2, 'months', 'after', ''),
    (26, 14, 'Reply to the Defence to the Application for the determination of damages', 1, 'months', 'after', ''),
    (27, 43, 'Rejoinder to the Reply to the Defence to the Application for the determination of damages', 1, 'months', 'after', ''),
    (28, 62, 'Defence to the Request to lay open books', 2, 'months', 'after', ''),
    (29, 76, 'Reply to the Defence to the Request to lay open books', 14, 'days', 'after', ''),
    (30, 40, 'Rejoinder to the Reply to the Defence to the Request to lay open books', 14, 'days', 'after', ''),
    (31, 37, 'Statement of Appeal against a decision referred to in Rule 220.1(a) and (b)', 2, 'months', 'after', ''),
    (32, 50, 'Statement of Appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3', 15, 'days', 'after', ''),
    (33, 37, 'Statement of grounds of appeal against a decision referred to in Rule 220.1(a) and (b)', 4, 'months', 'after', ''),
    (34, 50, 'Statement of grounds of appeal against an order referred to in Rule 220.1(c) or a decision referred to in Rule 220.2 or 221.3', 15, 'days', 'after', ''),
    (35, 23, 'Challenge of a decision to reject the appeal as inadmissible', 1, 'months', 'after', ''),
    (36, 89, 'Statement of response (to the Appeal pursuant to Rule 224.2(a))', 3, 'months', 'after', ''),
    (37, 60, 'Statement of response (to the Appeal pursuant to Rule 224.2(b))', 15, 'days', 'after', ''),
    (38, 89, 'Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))', 3, 'months', 'after', ''),
    (39, 60, 'Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))', 15, 'days', 'after', ''),
    (40, 87, 'Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(a))', 2, 'months', 'after', ''),
    (41, 90, 'Reply to a Statement of cross-appeal (to the Appeal pursuant to Rule 224.2(b))', 15, 'days', 'after', ''),
    (42, 98, 'Application for Rehearing (fundamental procedural defect)', 2, 'months', 'after', ''),
    (43, 88, 'Application for Rehearing (criminal offence)', 2, 'months', 'after', ''),
    (44, 78, 'Application for the review of a case management order', 15, 'days', 'after', ''),
    (45, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (46, 68, 'Correction of deficiencies / Submission of written comments', 14, 'days', 'after', ''),
    (47, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (48, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (49, 44, 'Request for review of the order to preserve evidence', 30, 'days', 'after', ''),
    (50, 26, 'Start of proceedings on the merits', 31, 'days', 'after', 'Or 20 Working days, whichever is longer'),
    (51, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (52, 66, 'Start of proceedings on the merits', 31, 'days', 'after', 'Or 20 Working days, whichever is longer'),
    (53, 100, 'Appeal (Orders & with leave)', 15, 'days', 'after', ''),
    (54, 99, 'Request for discretionary review', 15, 'days', 'after', ''),
    (55, 2, 'Application for leave to appeal against cost decisions', 15, 'days', 'after', ''),
    (56, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (57, 71, 'Correction of deficiencies / Payment', 14, 'days', 'after', ''),
    (58, 75, 'Request for a referral to the central division', 10, 'days', 'after', ''),
    (59, 45, 'Application to annul a decision of the EPO to reject a request for unitary effect', 3, 'weeks', 'after', ''),
    (60, 49, 'Information on engaging an interpreter at party''s expense', 2, 'weeks', 'before', ''),
    (61, 6, 'Counterclaim for Infringement', 2, 'months', 'after', ''),
    (62, 6, 'Application to amend the patent', 2, 'months', 'after', ''),
    (63, 5, 'Counterclaim for Revocation', 3, 'months', 'after', ''),
    (64, 104, 'Application for cost decision', 1, 'months', 'after', ''),
    (65, 104, 'Statement of Appeal against a decision referred to in Rule 220.1(a) and (b)', 2, 'months', 'after', ''),
    (66, 104, 'Statement of grounds of appeal against a decision referred to in Rule 220.1(a) and (b)', 4, 'months', 'after', ''),
    (67, 55, 'Renewal of Protective Letter', 6, 'months', 'after', ''),
    (68, 105, 'Rectification of decisions and orders', 1, 'months', 'after', ''),
    (69, 56, 'Organize Translation Issue', 2, 'weeks', 'after', ''),
    (70, 3, 'Application to request confidentiality from the public', 14, 'days', 'after', 'The 262.2 application can also be filed for opponent submissions to protect confidential information disclosed by the opponent.');

-- ============================================================================
-- Composite-rule wiring for R.198 / R.213 ("31 days OR 20 working_days,
-- whichever is longer"). Only the alt_* + combine_op fields are populated;
-- the base 31-day calculation is already in the row.
-- ============================================================================

UPDATE paliad.event_deadlines
   SET alt_duration_value = 20,
       alt_duration_unit  = 'working_days',
       combine_op         = 'max'
 WHERE id IN (50, 52);

-- ============================================================================
-- Seed: event_deadline_rule_codes (72 rows from youpc.data.deadline_rule_codes)
-- One audit fix applied: deadline 69's 'Rop.109' → 'RoP.109' (typo in source).
-- ============================================================================

INSERT INTO paliad.event_deadline_rule_codes (event_deadline_id, rule_code, sort_order) VALUES
    (1, 'RoP.019.1', 0),
    (2, 'RoP.023', 0),
    (3, 'RoP.029.a', 0),
    (4, 'RoP.029.b', 0),
    (5, 'RoP.029.c', 0),
    (6, 'RoP.029.a', 0),
    (6, 'RoP.030', 1),
    (7, 'RoP.029.d', 0),
    (7, 'RoP.032', 1),
    (8, 'RoP.029.e', 0),
    (9, 'RoP.032.1', 0),
    (10, 'RoP.032.3', 0),
    (11, 'RoP.032.3', 0),
    (12, 'RoP.039.1', 0),
    (13, 'RoP.049.1', 0),
    (14, 'RoP.051', 0),
    (15, 'RoP.052', 0),
    (16, 'RoP.056.1', 0),
    (17, 'RoP.056.3', 0),
    (18, 'RoP.056.4', 0),
    (19, 'RoP.067', 0),
    (20, 'RoP.069.1', 0),
    (21, 'RoP.069.2', 0),
    (22, 'RoP.088', 0),
    (23, 'RoP.109', 0),
    (24, 'RoP.118.4', 0),
    (25, 'RoP.137.2', 0),
    (26, 'RoP.139', 0),
    (27, 'RoP.139', 0),
    (28, 'RoP.142.2', 0),
    (29, 'RoP.142.3', 0),
    (30, 'RoP.142.3', 0),
    (31, 'RoP.224.1.a', 0),
    (32, 'RoP.224.1.b', 0),
    (33, 'RoP.224.2.a', 0),
    (34, 'RoP.224.2.b', 0),
    (35, 'RoP.234.1', 0),
    (36, 'RoP.235.1', 0),
    (37, 'RoP.235.2', 0),
    (38, 'RoP.237', 0),
    (39, 'RoP.237', 0),
    (40, 'RoP.238.1', 0),
    (41, 'RoP.238.2', 0),
    (42, 'RoP.245.2.a', 0),
    (43, 'RoP.245.2.b', 0),
    (44, 'RoP.333.2', 0),
    (45, 'RoP.016.3.a', 0),
    (46, 'RoP.019.5', 0),
    (47, 'RoP.027.2', 0),
    (48, 'RoP.089.2', 0),
    (49, 'RoP.197.3', 0),
    (50, 'RoP.198', 0),
    (51, 'RoP.207.6.a', 0),
    (52, 'RoP.213', 0),
    (53, 'RoP.220.2', 0),
    (54, 'RoP.220.3', 0),
    (55, 'RoP.221.1', 0),
    (56, 'RoP.229.2', 0),
    (57, 'RoP.253.2', 0),
    (58, 'RoP.321.3', 0),
    (59, 'RoP.097.1', 0),
    (60, 'RoP.109.4', 0),
    (61, 'RoP.049.2.b', 0),
    (62, 'RoP.049.2.a', 0),
    (63, 'RoP.025', 0),
    (64, 'RoP.151', 0),
    (65, 'RoP.224.1.a', 0),
    (66, 'RoP.224.2.a', 0),
    (67, 'RoP.207.9', 0),
    (68, 'RoP.353', 0),
    (69, 'RoP.109', 0),  -- youpc had 'Rop.109' (lowercase typo); normalised on import
    (70, 'RoP.262.2', 0);

-- ============================================================================
-- Sequence positioning: not needed — id columns are plain bigint, not
-- serial. PR-2 admin/edit flows (if any) will assign ids explicitly.
-- ============================================================================
