"use client";

import React, { useState, useEffect } from "react";
import DashboardLayout from "@/components/layout/DashboardLayout";
import { useGlobalContext } from "@/context/Provider";
import apis, { referralUserType } from "@/utils/apis";
import { Check, Loader2 } from "lucide-react";
import { useRouter } from "next/navigation";

const MedalBadge = ({ level, locked = false }: { level: number; locked?: boolean }) => {
  return (
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 881 786" fill="none" className={`w-44 h-44 md:w-52 md:h-52 drop-shadow-md ${locked ? "opacity-85" : ""}`}>
      <path d="M189.852 545.51L185.412 555.001L182.598 552.438C180.683 550.698 178.178 549.979 175.644 550.813L161.268 555.542C155.74 557.355 150.163 557.949 144.347 558.355C120.951 559.98 100.298 548.333 84.5447 530.936L77.5608 522.664C89.0942 518.549 100.378 515.497 112.301 515.466C121.799 515.434 130.658 518.101 139.438 521.487C147.31 524.519 154.174 528.842 161.377 532.905C146.871 516.622 133.781 500.475 121.529 483.036C119.813 480.598 117.549 479.608 114.805 479.816L99.3306 481.015C76.0544 482.817 54.8334 470.805 40.6063 451.762C34.7897 443.98 30.1503 435.896 26.0298 426.947C34.5103 426.926 42.5717 426.468 51.0222 427.916C67.4243 430.718 82.1602 438.75 94.6414 449.98L104.997 460.367L96.9661 444.969L90.092 430.749L77.8802 403.049C76.3736 399.632 74.1288 397.496 70.6269 396.944C56.5494 394.725 42.1228 391.058 30.9885 381.557C12.8105 366.045 4.94857 346.075 0 322.01C20.0237 326.135 36.4558 334.678 50.0045 350.262C56.4297 357.649 61.049 365.472 66.1373 374.213C61.4581 354.357 57.3276 335.782 54.7935 316.228C54.4044 313.207 52.6385 310.654 50.3238 309.384L40.8158 304.154C22.3584 293.997 9.60785 276.172 5.27785 254.774C2.88338 242.94 2.10518 231.303 2.53419 218.729C20.0537 228.761 33.1934 239.96 41.9831 258.66C46.3929 268.036 48.7774 277.505 51.1918 288.048C51.2617 268.911 51.6707 250.159 53.8855 231.105C54.2148 228.303 53.5863 225.292 51.8204 223.396L44.4274 215.406C36.9846 207.363 30.6592 198.581 26.9278 187.934C19.8242 167.682 23.0967 145.555 30.1405 125.168C39.3592 135.679 48.0091 148.983 52.2793 162.401C56.6492 176.121 57.4274 189.966 57.0782 204.676C59.2333 194.998 61.3084 185.736 64.2117 176.225C65.6184 171.62 67.7435 167.099 68.2224 162.317C69.0904 153.66 63.204 136.336 71.2055 113.167C79.207 89.998 92.0573 79.8095 110.854 67.5062C111.473 80.9346 111.183 94.0088 108.938 107.187C106.494 121.532 100.478 134.158 91.1395 144.878L75.4158 161.744C73.6997 163.588 72.5624 165.484 71.8441 167.985C67.5939 182.903 64.1219 197.675 62.356 213.635C66.5662 202.884 71.7642 193.456 78.9277 184.663C85.4327 176.673 93.7335 170.797 102.992 166.641C109.856 163.557 116.68 161.151 124.203 160.359C118.656 179.965 108.809 200.3 93.2048 211.593C84.5049 217.896 74.8771 221.636 64.7505 224.552C62.0268 225.344 59.772 226.938 59.4428 230.095C57.6769 246.94 57.1979 263.608 58.2555 280.527L60.0214 296.268C59.5625 297.101 60.6001 297.455 60.64 296.393C62.2562 284.246 65.5586 272.807 71.2754 262.15C80.1749 245.586 93.4541 236.033 110.006 227.834C109.148 249.951 104.698 271.849 90.4611 288.413C84.2953 295.591 77.0819 300.841 69.4296 306.05L63.3537 310.186C61.0191 311.769 60.7497 315.228 61.2086 318.218C63.5232 333.553 66.7459 348.502 71.5049 363.201L77.9699 379.859C77.5509 369.525 77.1219 360.409 78.7482 350.7C82.4297 328.719 93.2347 313.405 109.776 299.414C114.406 322.437 115.104 346.814 104.519 367.17C99.6299 376.588 93.1948 384.609 86.1311 392.162C83.6369 394.829 83.2578 398.257 84.6346 401.632C89.5233 413.581 95.2101 424.801 101.426 436.094C106.015 444.438 110.744 452.074 116.611 459.929C108.918 436.291 107.302 411.664 118.596 389.714C121.968 383.172 125.49 377.13 130.289 371.16C137.802 388.204 143.449 406.914 142.012 425.186C141.134 436.354 138.231 446.824 133.432 456.731L127.066 469.888C125.67 472.764 125.51 475.702 127.436 478.285L138.999 493.776L147.479 503.735L164.6 521.716L172.192 528.696C172.721 529.644 173.639 528.727 172.811 528.061L168.591 520.675C158.075 502.246 153.306 479.004 157.935 457.919L164.221 435.479C179.914 454.679 191.129 476.962 190.999 502.392C190.939 514.07 189.013 525.06 186.759 536.322C186.06 539.791 186.819 543.114 189.832 545.531L189.852 545.51Z" fill={locked ? "url(#silver_paint0)" : "url(#paint0_linear_89_4565)"} />
      <path d="M766.957 552.919C748.697 561.327 728.219 559.558 709.739 552.18C703.366 549.641 700.579 550.151 695.704 554.74L691.329 545.676C694.426 542.69 694.915 539.235 694.036 535.25C691.209 522.357 689.441 509.526 690.161 496.207C691.389 473.491 702.377 452.898 716.622 435.561L722.585 456.633C724.903 464.812 725.112 473.085 724.473 481.701C723.594 493.533 720.268 504.636 715.293 515.218L708.301 528.673C710.249 527.57 711.677 526.353 713.115 524.854L734.502 502.471C741.524 495.125 747.558 487.279 753.252 478.891C755.329 475.832 755.279 472.71 753.721 469.453L746.059 453.491C733.703 425.384 738.308 398.725 750.654 371.295C772.301 399.651 775.148 425.301 764.359 459.932C777.285 441.68 787.804 422.273 796.365 401.524C797.673 398.35 797.283 395.02 795.036 392.543C779.013 374.874 769.294 360.16 768.215 334.968C767.706 323.043 768.435 311.378 771.212 299.671C777.375 304.884 782.28 310.243 786.995 316.143C801.978 334.957 804.735 356.539 802.847 381.024L810.11 361.617C814.525 347.507 817.481 333.272 819.749 318.641C820.348 314.749 819.15 311.544 815.973 309.317L801.079 298.891C779.183 281.346 771.731 256.643 771.192 228.411C780.002 232.792 787.844 237.225 794.766 243.49C811.008 258.193 817.162 276.185 821.088 298.193L823.125 278.308C823.844 262.668 823.435 247.236 821.677 231.752C821.217 227.662 818.52 225.56 814.994 224.52C805.604 221.752 796.744 218.141 788.583 212.543C772.51 201.533 762.492 180.565 756.858 160.7C787.614 165.789 806.443 183.146 818.161 212.969C817.921 213.947 818.96 214.509 818.87 213.5C816.442 196.934 813.226 181.19 808.161 165.664L804.196 160.888L790.701 146.361C771.232 125.404 769.094 97.6512 770.033 69.5553C769.604 68.8685 770.263 68.2441 770.782 68.6188L787.075 80.419C803.896 94.6022 813.726 115.601 814.095 138.224L812.647 161.699C812.497 164.134 813.625 166.725 814.385 169.036C818.21 180.648 821.197 192.053 823.775 204.031C823.745 190.118 824.384 177.131 828.31 164.124C832.235 151.117 841.215 136.569 850.845 125.83C860.384 152.605 862.173 179.91 846.53 203.427C842.015 210.201 836.77 215.956 830.997 221.564C828.18 224.301 826.781 227.267 827.261 231.471C828.909 245.862 829.308 260.045 829.768 274.541L830.177 287.715C832.635 274.26 836.91 261.461 844.122 249.848C848.917 242.116 854.861 235.873 861.773 230.233L877.486 219.514C877.676 219.379 877.935 218.973 878.145 219.057C878.395 219.15 878.515 219.462 878.525 219.962C879.034 246.934 875.658 273.344 856.189 292.512C851.454 297.163 846.579 301.117 840.866 304.166L830.597 309.661C828.319 309.661 826.571 313.167 826.232 316.133C823.954 335.613 819.669 354.073 815.224 373.865C820.498 364.635 825.572 356.539 832.445 348.943C845.141 334.916 861.643 326.133 879.903 322.574C880.193 322.522 881.142 322.221 880.982 322.991C878.714 333.667 875.927 343.813 871.352 353.657C864.989 367.33 855.59 379.214 842.844 386.799C832.775 392.804 821.577 395.301 810.339 397.153C806.873 397.726 804.665 399.391 803.367 402.512L797.173 417.382L779.143 455.187L776.896 459.297C776.087 459.432 776.506 460.546 777.016 460.13C780.102 455.801 783.668 452.637 787.354 449.235C799.95 437.58 818.75 428.35 835.582 427.268L855.11 427.289C846.28 446.04 834.333 463.023 817.082 472.908C806.134 479.183 793.947 482.013 781.511 481.066L765.528 479.859C762.991 479.672 761.253 480.837 759.585 482.939L719.748 533.054L737.868 523.064C748.297 518.434 762.861 514.396 773.829 515.687C783.748 516.852 793.288 518.871 802.667 522.565C803.287 522.357 803.637 523.116 803.107 523.314L798.442 528.933L791.67 535.978C784.597 543.346 776.236 548.673 766.997 552.929L766.997 552.929Z" fill={locked ? "url(#silver_paint1)" : "url(#paint1_linear_89_4565)"} />
      <path d="M725.73 675.926L703.09 673.216L647.16 666.696C645.49 666.506 644.01 666.486 643.09 668.136L595.45 754.096L594.49 753.286C567.31 700.126 540.2 646.786 513.17 593.256L513.85 592.286L515.89 591.086C564.04 578.516 608.93 552.726 644.23 520.416L645.33 520.796L646.39 521.306L724.51 674.396C725.65 674.506 726.31 674.706 725.74 675.926H725.73Z" fill={locked ? "url(#silver_paint2)" : "url(#paint2_linear_89_4565)"} />
      <path d="M667.87 498.396C668 499.116 667.63 499.726 666.75 500.226L659.31 508.146C658.84 509.316 658.16 509.726 657.28 509.386L657.52 510.476C650.18 517.536 646.51 521.066 646.51 521.066L645.33 520.806L628.85 534.876C595.11 562.266 555.86 581.846 513.85 592.296C513.89 592.686 513.9 593.286 513.64 593.366C510.87 594.276 501.39 596.676 498.8 596.786L497.87 595.976C497.9 596.366 497.88 596.966 497.61 597.046C495.62 597.606 485.25 599.806 483.75 599.696L482.84 598.856L468.76 571.026C504.24 567.106 536.58 557.976 568.19 542.116C601.3 525.496 630.77 503.256 656.45 476.046L667.87 498.396Z" fill={locked ? "#4B5563" : "#6637B8"} />
      <path d="M513.85 592.296L515.26 595.676L563.17 689.656L594.14 750.106C594.9 751.586 595.71 752.506 595.45 754.116L586.56 770.176L585.47 770.276C555.65 712.886 526.13 655.056 496.91 596.786L497.86 595.986L513.84 592.306L513.85 592.296Z" fill={locked ? "#9CA3AF" : "#A879E9"} />
      <path d="M740.63 677.556L725.73 675.926C725.63 675.446 724.71 675.456 723.88 675.386L709.79 647.476L646.19 523.286C645.66 522.246 644.03 519.469 644 518.509L648.252 515.506L655.5 506.327L658.311 509.226L743.48 676.206C744.01 677.416 743.06 677.866 740.62 677.536L740.63 677.556Z" fill={locked ? "#9CA3AF" : "#A778E9"} />
      <path d="M586.57 770.156L578.07 785.666L547.95 726.856L482.84 598.846L497.87 595.966C497.79 596.826 498.15 597.286 498.67 598.306L549.34 697.966L581.57 761.146L586.56 770.166L586.57 770.156Z" fill={locked ? "url(#silver_paint3)" : "url(#paint3_linear_89_4565)"} />
      <path d="M666.5 494.667L675.91 514.036L707.04 574.816L761.04 679.846L740.62 677.556C741.42 677.446 742.06 677.496 742.86 677.226L705.34 603.426L655.37 507.506L658.108 506.006L663 498.697C663.59 498.077 662.89 497.301 663.5 497.221L666.5 494.667Z" fill={locked ? "url(#silver_paint4)" : "url(#paint4_linear_89_4565)"} />
      <path d="M370.03 592.286C342.15 646.026 314.23 699.736 286.28 753.396L285.11 753.006L237.58 667.836C236.69 666.236 234.77 666.646 233.26 666.816L155.42 675.916C154.96 675.186 155.29 674.616 156.41 674.196C182.91 622.976 209.22 571.576 235.34 520.006L236.56 519.796C237.88 520.366 239.22 520.966 240.33 521.896C255.25 534.376 268.82 545.696 285.46 556.066C311.2 572.106 338.77 583.706 368.13 591.176L370.03 592.286Z" fill={locked ? "url(#silver_paint5)" : "url(#paint5_linear_89_4565)"} />
      <path d="M398.95 598.906L385.44 596.696L384.9 595.586L383.97 596.406L370.52 593.416L370.02 592.286C319.92 581.156 273.98 554.576 236.54 519.796C236.24 520.046 235.78 520.396 235.54 520.236C233.14 518.616 226.37 511.946 224.3 509.506L224.49 508.296L223.38 508.486L214.13 498.466L213.73 497.386L225.31 474.966L240.19 489.716C253.63 503.046 268.65 514.386 284.56 524.786C323.37 550.146 367.16 566.006 413.49 570.796L398.95 598.916V598.906Z" fill={locked ? "#4B5563" : "#6637B9"} />
      <path d="M370.03 592.286L384.9 595.586L385.63 596.356L296.81 769.206C296.17 770.406 295.33 771.236 294.35 769.916L285.11 753.006C285.88 752.516 286.13 752.166 286.54 751.366L321.68 683.106L368.12 593.676C368.43 592.506 369.21 592.266 370.03 592.276V592.286Z" fill={locked ? "url(#silver_paint6)" : "url(#paint6_linear_89_4565)"} />
      <path d="M238 518.124L231.35 531.518L169.866 650.48L157.214 675.703C157 675.761 156 675.791 155.196 676.006L140.379 677.318C139.078 677.541 136.739 677.874 137.024 676.249L224.548 506.597L225.738 506.506L233.231 513.733L237.99 518.124H238Z" fill={locked ? "url(#silver_paint7)" : "url(#paint7_linear_89_4565)"} />
      <path d="M225.99 505.575L188.335 579.08L138.295 676.8C138.12 677.104 137.26 677.087 138 677.006C137.351 677.655 138.5 677.434 137.507 677.545L120 679.506L170.973 580.469L215.084 494.506C216.148 494.537 216.908 495.277 217.719 496.158L224.044 503L226 505.564L225.99 505.575Z" fill={locked ? "url(#silver_paint8)" : "url(#paint8_linear_89_4565)"} />
      <path d="M398.95 598.906L380.95 633.956L315.27 762.146L302.97 785.716L294.34 769.926C295.02 769.866 295.45 769.656 295.92 768.736L342.16 678.626L384.89 595.586L398.41 597.896C399 597.996 399.08 598.616 398.94 598.906H398.95Z" fill={locked ? "url(#silver_paint9)" : "url(#paint9_linear_89_4565)"} />
      <path d="M400.48 605.616C386.21 603.276 373.1 600.356 359.6 596.146C308.04 580.086 262.48 549.436 226.58 509.216C191.57 469.996 165.49 419.926 154.41 368.366C139.71 299.916 147.75 228.646 178.92 165.866C191.8 139.916 208.02 115.946 227.67 94.6862L247.2 75.1862C280.33 45.6962 318.85 23.4263 361.66 11.1562C389.87 3.06625 420.37 -0.763748 449.76 0.126252C519 2.22625 583.2 28.5463 634.91 74.3863L655.69 95.1462C667.23 107.916 677.78 121.186 687.08 135.756C703.46 161.426 716.07 189.046 724.39 218.386C732.08 245.476 735.56 273.196 735.68 301.406C735.82 336.036 730.34 370.246 718.91 402.886C701.96 451.276 673.59 494.666 636.28 529.736C622.07 543.096 606.45 554.986 589.94 565.396C555.81 586.916 517.91 600.696 477.9 606.386C452.25 610.036 426.31 609.856 400.47 605.606L400.48 605.616Z" fill={locked ? "url(#silver_paint10)" : "url(#paint10_linear_89_4565)"} />
      <path d="M432.06 561.046C388.68 558.946 347.2 545.376 311.25 521.626C300.4 514.466 290.27 506.876 280.7 498.136L260.7 478.136C251.4 468.016 243.61 457.266 235.98 445.846C228.35 434.426 222.49 423.176 216.9 410.896C209.47 394.576 203.71 378.016 199.97 360.366C195.62 339.846 193.15 319.166 193.73 298.196C194.68 263.706 201.63 229.996 215.47 198.406C227.46 171.016 243.98 146.096 264.43 124.486L273.2 115.596C278.58 110.146 284.27 105.586 290.31 100.686C309.45 85.1763 330.65 73.0863 353.59 64.0563C367.21 58.6963 380.81 54.9663 395.29 52.1363C426.05 46.1263 457.67 46.2163 488.38 52.3963C500.37 54.8063 511.62 57.7063 523.04 61.8663C587.78 85.4863 638.71 134.926 666.56 197.726C678.8 225.326 686.53 256.976 688.15 287.106C689.53 312.826 688.01 337.376 682.08 362.616C671.44 407.856 649.06 449.756 617.08 483.496C590.35 511.696 557.68 533.686 521.16 546.996C498.5 555.256 475.01 559.826 451.05 561.006L432.09 561.036L432.06 561.046Z" fill={locked ? "url(#silver_paint11)" : "url(#paint11_linear_89_4565)"} />
      <path d="M299.12 478.986L281.44 461.346C245.37 421.346 224.54 372.096 220.72 318.276L220.76 291.156C222.51 260.546 229.82 231.116 242.87 203.296C252.69 182.366 265.7 163.286 281.15 146.166L296.69 130.646C325.51 104.266 360.66 86.5763 399.01 78.9663C411.52 76.4863 423.59 74.9263 436.3 74.6263C492.74 73.3063 547.25 94.4363 588.41 132.866C640.18 181.206 666.96 251.186 661.44 321.986C658.67 357.476 647.95 391.626 629.93 422.416C592.88 485.716 527.36 529.406 453.51 533.956L429.86 533.976C403.12 532.296 377.2 525.806 352.93 514.416C333.27 505.196 315.46 493.576 299.12 478.986Z" fill={locked ? "url(#silver_paint12)" : "url(#paint12_linear_89_4565)"} />

      {/* Dynamic Text for Badge Number */}
      <text x="440" y="445" fill={locked ? "#E5E7EB" : "#FEFEFE"} fontSize="210" fontWeight="900" textAnchor="middle" fontFamily="Outfit, Inter, sans-serif">{level}</text>

      {/* Ribbon Letters (LEVEL text) */}
      <path d="M382.79 202.806L382.85 213.596L345.98 213.606V153.326L357.94 153.296L357.97 202.846L382.79 202.806Z" fill={locked ? "#E5E7EB" : "#FEFEFE"} />
      <path d="M461.94 170.456L473.9 170.286L459.24 213.606L446.18 213.456L431.07 170.426L443.28 170.346L452.69 201.606L461.94 170.456Z" fill={locked ? "#E5E7EB" : "#FEFEFE"} />
      <path d="M536.85 153.176H525.03V213.636H536.85V153.176Z" fill={locked ? "#E5E7EB" : "#FEFDFE"} />
      <path d="M478.5 183.467C481.28 174.057 490.29 168.577 499.92 169.427C512.93 170.577 518.861 182.156 517.491 194.746H488.32C488.35 199.626 490.991 203.737 495.201 204.957C499.63 206.236 504.439 204.906 506.809 200.168L516.97 201.156C516.05 205.656 513 209.496 508.82 211.786C498.74 217.296 484.12 213.996 479.36 202.996C476.7 196.846 476.621 189.836 478.5 183.467ZM501.241 178.766C498.051 177.386 494.55 178.006 491.84 180.316C489.8 182.056 488.701 184.596 488.491 187.266L506.621 187.127V187.116C506.55 182.536 504.16 180.036 501.241 178.766Z" fill={locked ? "#E5E7EB" : "#FEFEFE"} />
      <path d="M408.42 169.347C417.06 169.277 424.311 173.946 427.241 182.146C428.681 186.176 428.89 190.457 428.46 194.757L398.98 194.776C399.15 199.546 401.7 203.736 405.72 204.936C410.4 206.336 415.26 204.886 417.48 200.266V200.257L427.781 201.236C426.241 207.026 422.01 211.236 416.6 213.076C404.93 217.046 391.73 211.726 388.82 199.496C387.69 194.746 387.671 189.906 388.661 185.137C390.641 175.557 398.72 169.427 408.42 169.347ZM408.22 178.087C403.27 178.237 399.341 182.267 399.321 187.266L417.35 187.217V187.207C417.43 181.557 413.17 177.937 408.22 178.087Z" fill={locked ? "#E5E7EB" : "#FEFEFE"} />

      {locked && (
        <g transform="translate(380, 520) scale(2.8)" filter="url(#lockShadowSilver)">
          <circle cx="21" cy="21" r="20" fill="#1c1d22" stroke="#ffffff" strokeWidth="2" />
          <path d="M15 19 V15 C15 12 17 10 21 10 C25 10 27 12 27 15 V19 M13 19 H29 V28 H13 Z" stroke="#ffffff" strokeWidth="2" fill="none" strokeLinecap="round" strokeLinejoin="round" />
          <circle cx="21" cy="24" r="1.5" fill="#ffffff" />
        </g>
      )}
      <defs>
        {locked && (
          <filter id="lockShadowSilver" x="-20%" y="-20%" width="140%" height="140%">
            <feDropShadow dx="0" dy="4" stdDeviation="3" floodColor="#000" floodOpacity="0.4" />
          </filter>
        )}
        <linearGradient id="paint0_linear_89_4565" x1="95.5" y1="67.5062" x2="187.639" y2="573.038" gradientUnits="userSpaceOnUse">
          <stop stopColor="#C298FB" />
          <stop offset="0.5" stopColor="#A779EE" />
          <stop offset="1" stopColor="#8552DE" />
        </linearGradient>
        <linearGradient id="paint1_linear_89_4565" x1="785.5" y1="68.5062" x2="785.5" y2="558.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#BD93F9" />
          <stop offset="1" stopColor="#8957E0" />
        </linearGradient>
        <linearGradient id="paint2_linear_89_4565" x1="579" y1="545.506" x2="669.5" y2="715.006" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B24AD" />
          <stop offset="0.5" stopColor="#A26FEB" />
          <stop offset="1" stopColor="#8E5BE2" />
        </linearGradient>
        <linearGradient id="paint3_linear_89_4565" x1="534.705" y1="595.966" x2="534.705" y2="785.666" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B24AD" />
          <stop offset="0.5" stopColor="#A26FEB" />
          <stop offset="1" stopColor="#6336C9" />
        </linearGradient>
        <linearGradient id="paint4_linear_89_4565" x1="709.145" y1="498.386" x2="709.145" y2="679.846" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B24AD" />
          <stop offset="0.5" stopColor="#A26FEB" />
          <stop offset="1" stopColor="#6336C9" />
        </linearGradient>
        <linearGradient id="paint5_linear_89_4565" x1="307.727" y1="556.006" x2="214.196" y2="718.006" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B24AE" />
          <stop offset="0.5" stopColor="#A775EC" />
          <stop offset="1" stopColor="#8651DE" />
        </linearGradient>
        <linearGradient id="paint6_linear_89_4565" x1="335.37" y1="592.276" x2="335.37" y2="770.589" gradientUnits="userSpaceOnUse">
          <stop stopColor="#6E40CB" />
          <stop offset="0.5" stopColor="#E8D4FD" />
          <stop offset="1" stopColor="#9B67F3" />
        </linearGradient>
        <linearGradient id="paint7_linear_89_4565" x1="187.5" y1="506.506" x2="187.5" y2="677.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#8957DD" />
          <stop offset="0.5" stopColor="#EDDBFD" />
          <stop offset="1" stopColor="#A16CF5" />
        </linearGradient>
        <linearGradient id="paint8_linear_89_4565" x1="173" y1="494.506" x2="173" y2="679.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#5329B4" />
          <stop offset="0.5" stopColor="#B284F0" />
          <stop offset="1" stopColor="#6D3AD1" />
        </linearGradient>
        <linearGradient id="paint9_linear_89_4565" x1="346.67" y1="595.586" x2="346.67" y2="785.716" gradientUnits="userSpaceOnUse">
          <stop stopColor="#5329B4" />
          <stop offset="0.5" stopColor="#B284F0" />
          <stop offset="1" stopColor="#6D3AD1" />
        </linearGradient>
        <linearGradient id="paint10_linear_89_4565" x1="441.49" y1="0" x2="441.49" y2="608.97" gradientUnits="userSpaceOnUse">
          <stop stopColor="#ECD7FD" />
          <stop offset="1" stopColor="#9460E5" />
        </linearGradient>
        <linearGradient id="paint11_linear_89_4565" x1="441.135" y1="47.6941" x2="441.135" y2="561.046" gradientUnits="userSpaceOnUse">
          <stop stopColor="#733FD1" />
          <stop offset="0.456315" stopColor="#945FE5" />
          <stop offset="1" stopColor="#E3C8FC" />
        </linearGradient>
        <linearGradient id="paint12_linear_89_4565" x1="441" y1="63.5062" x2="441.436" y2="533.976" gradientUnits="userSpaceOnUse">
          <stop stopColor="#CFACFB" />
          <stop offset="0.498469" stopColor="#8E59E2" />
          <stop offset="1" stopColor="#6233C6" />
        </linearGradient>
        <linearGradient id="silver_paint0" x1="95.5" y1="67.5062" x2="187.639" y2="573.038" gradientUnits="userSpaceOnUse">
          <stop stopColor="#E5E7EB" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#4B5563" />
        </linearGradient>
        <linearGradient id="silver_paint1" x1="785.5" y1="68.5062" x2="785.5" y2="558.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#D1D5DB" />
          <stop offset="1" stopColor="#6B7280" />
        </linearGradient>
        <linearGradient id="silver_paint2" x1="579" y1="545.506" x2="669.5" y2="715.006" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#6B7280" />
        </linearGradient>
        <linearGradient id="silver_paint3" x1="534.705" y1="595.966" x2="534.705" y2="785.666" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#4B5563" />
        </linearGradient>
        <linearGradient id="silver_paint4" x1="709.145" y1="498.386" x2="709.145" y2="679.846" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#4B5563" />
        </linearGradient>
        <linearGradient id="silver_paint5" x1="307.727" y1="556.006" x2="214.196" y2="718.006" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#6B7280" />
        </linearGradient>
        <linearGradient id="silver_paint6" x1="335.37" y1="592.276" x2="335.37" y2="770.589" gradientUnits="userSpaceOnUse">
          <stop stopColor="#6B7280" />
          <stop offset="0.5" stopColor="#E5E7EB" />
          <stop offset="1" stopColor="#9CA3AF" />
        </linearGradient>
        <linearGradient id="silver_paint7" x1="187.5" y1="506.506" x2="187.5" y2="677.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#6B7280" />
          <stop offset="0.5" stopColor="#F3F4F6" />
          <stop offset="1" stopColor="#9CA3AF" />
        </linearGradient>
        <linearGradient id="silver_paint8" x1="173" y1="494.506" x2="173" y2="679.506" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#4B5563" />
        </linearGradient>
        <linearGradient id="silver_paint9" x1="346.67" y1="595.586" x2="346.67" y2="785.716" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.5" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#4B5563" />
        </linearGradient>
        <linearGradient id="silver_paint10" x1="441.49" y1="0" x2="441.49" y2="608.97" gradientUnits="userSpaceOnUse">
          <stop stopColor="#F3F4F6" />
          <stop offset="1" stopColor="#9CA3AF" />
        </linearGradient>
        <linearGradient id="silver_paint11" x1="441.135" y1="47.6941" x2="441.135" y2="561.046" gradientUnits="userSpaceOnUse">
          <stop stopColor="#4B5563" />
          <stop offset="0.456315" stopColor="#9CA3AF" />
          <stop offset="1" stopColor="#E5E7EB" />
        </linearGradient>
        <linearGradient id="silver_paint12" x1="441" y1="63.5062" x2="441.436" y2="533.976" gradientUnits="userSpaceOnUse">
          <stop stopColor="#D1D5DB" />
          <stop offset="0.498469" stopColor="#6B7280" />
          <stop offset="1" stopColor="#374151" />
        </linearGradient>
      </defs>
    </svg>
  );
};

const GreenCheckIcon = () => (
  <span className="flex-shrink-0 bg-[#4CAF50] text-white rounded-full w-5 h-5 flex items-center justify-center shadow-sm">
    <Check size={12} strokeWidth={4} />
  </span>
);

const YellowLockIcon = () => (
  <span className="inline-flex items-center justify-center bg-[#FFC72C] text-white rounded-full w-[21px] h-[21px] flex-shrink-0 shadow-sm ml-2">
    <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.5" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="11" width="18" height="11" rx="2" ry="2" />
      <path d="M7 11V7a5 5 0 0 1 10 0v4" />
    </svg>
  </span>
);

const NoLevelMedalIcon = () => (
  <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" className="w-12 h-12 drop-shadow-md">
    <path d="M16 28 L12 44 L20 40 L24 28 Z" fill="#4B68FF" />
    <path d="M32 28 L36 44 L28 40 L24 28 Z" fill="#3B55D9" />
    <path d="M20 28 L24 40 L28 28 Z" fill="#2A3EA6" />
    <circle cx="24" cy="20" r="16" fill="url(#goldGrad)" stroke="#E5A900" strokeWidth="1.5" />
    <circle cx="24" cy="20" r="12" fill="url(#goldInnerGrad)" />
    <path d="M24 11 L27.2 17.5 L34.3 18.5 L29.2 23.5 L30.4 30.6 L24 27.2 L17.6 30.6 L18.8 23.5 L13.7 18.5 L20.8 17.5 Z" fill="#FFF" />
    <defs>
      <linearGradient id="goldGrad" x1="8" y1="4" x2="40" y2="36" gradientUnits="userSpaceOnUse">
        <stop stopColor="#FFE066" />
        <stop offset="0.5" stopColor="#F5B800" />
        <stop offset="1" stopColor="#C28F00" />
      </linearGradient>
      <linearGradient id="goldInnerGrad" x1="12" y1="8" x2="36" y2="32" gradientUnits="userSpaceOnUse">
        <stop stopColor="#FFF2B2" />
        <stop offset="1" stopColor="#E5A900" />
      </linearGradient>
    </defs>
  </svg>
);

const Rewards = () => {
  const router = useRouter();
  const { Toast } = useGlobalContext();

  const [referredCustomers, setReferredCustomers] = useState(0);
  const [referredVendors, setReferredVendors] = useState(0);
  const [referredDrivers, setReferredDrivers] = useState(0);
  const [targetCustomers, setTargetCustomers] = useState(4);
  const [targetVendors, setTargetVendors] = useState(4);
  const [targetDrivers, setTargetDrivers] = useState(4);
  const [nextLevelNum, setNextLevelNum] = useState(1);
  const [rewardAmount, setRewardAmount] = useState(0);
  const [currentLevel, setCurrentLevel] = useState(0);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchReferralStats = async () => {
      try {
        setLoading(true);
        const res = await apis.Sales.getCertificationListing();
        const apiData = res?.data || res;

        if (apiData) {
          setCurrentLevel(apiData.current_level ?? 0);

          if (apiData.current_progress) {
            setReferredCustomers(apiData.current_progress.referred_customers ?? 0);
            setReferredVendors(apiData.current_progress.referred_vendors ?? 0);
            setReferredDrivers(apiData.current_progress.referred_drivers ?? 0);
            setRewardAmount(apiData.current_progress.reward_earned ?? 0);
          }

          if (apiData.next_level) {
            setTargetCustomers(apiData.next_level.customer_required ?? 4);
            setTargetVendors(apiData.next_level.vendor_required ?? 4);
            setTargetDrivers(apiData.next_level.driver_required ?? 4);
            setNextLevelNum(apiData.next_level.level ?? 1);
          } else {
            setNextLevelNum(0);
          }
        }
      } catch (err) {
        console.error("Error fetching referral stats, using screen defaults:", err);
      } finally {
        setLoading(false);
      }
    };

    fetchReferralStats();
  }, []);

  const customerProgress = targetCustomers > 0 ? Math.min((referredCustomers / targetCustomers) * 100, 100) : 0;
  const vendorProgress = targetVendors > 0 ? Math.min((referredVendors / targetVendors) * 100, 100) : 0;
  const driverProgress = targetDrivers > 0 ? Math.min((referredDrivers / targetDrivers) * 100, 100) : 0;

  return (
    <div className="flex flex-col min-h-screen p-5 md:p-8 max-w-5xl mx-auto space-y-6 select-none">
      {/* Main Title & Description */}
      <div className="space-y-1">
        <h1 className="text-3xl md:text-4xl font-extrabold text-gray-900 tracking-tight">Rewards & Levels</h1>
        <p className="text-gray-400 text-sm md:text-base font-medium">
          Track your current Certificate Level and see what you need to do next.
        </p>
      </div>

      {loading ? (
        <div className="flex items-center justify-center py-20">
          <Loader2 className="w-8 h-8 text-indigo-600 animate-spin" />
          <span className="ml-3 text-gray-600 font-medium">Loading your rewards dashboard...</span>
        </div>
      ) : (
        <div className="space-y-6">
          {/* Current Certificate Level Section */}
          <div className="space-y-4">
            <h2 className="text-[17px] font-bold text-gray-900 tracking-tight">Current Certificate Level</h2>

            {currentLevel === 0 ? (
              /* No Certificate Level Yet Card matching screenshot */
              <div
                className="border border-[#FFA458]/30 rounded-[24px] p-8 flex flex-col items-center justify-center relative overflow-hidden shadow-sm hover:shadow-md transition-all duration-300 min-h-[220px]"
                style={{ background: 'linear-gradient(90deg, #FFA458 0%, #FFDBB2 52%, #FFBA81 100%)' }}
              >
                <div className="mb-4">
                  <NoLevelMedalIcon />
                </div>
                <h3 className="text-xl md:text-2xl font-extrabold text-gray-900 text-center">
                  No Certificate Level Yet
                </h3>
                <p className="text-[13px] md:text-[14px] text-gray-800 font-semibold text-center max-w-md leading-relaxed mt-2">
                  Start Your Rewards Journey Unlock Your First Level <br className="hidden sm:inline" /> And Begin Earning Rewards.
                </p>
              </div>
            ) : (
              /* Unlocked Certificate Level Card */
              <div
                className="border border-[#e2d9fc] rounded-[24px] p-6 md:p-8 flex flex-col md:flex-row justify-between items-center relative overflow-hidden shadow-sm hover:shadow-md transition-all duration-300"
                style={{ background: 'linear-gradient(92.82deg, #EDE5F8 15.36%, #EAF3FF 63.82%, #FDF3FF 95.19%)' }}
              >
                {/* Card Left Details */}
                <div className="flex-1 space-y-5 w-full text-left">
                  <div>
                    <p className="text-[11px] uppercase tracking-wider text-gray-400 font-semibold mb-1">Current Level</p>
                    <p className="text-3xl md:text-4xl font-extrabold text-[#2a138c]">Level {currentLevel}</p>
                  </div>

                  {/* Requirements checkmarks list in 2 columns */}
                  <div className="grid grid-cols-1 sm:grid-cols-2 gap-x-8 gap-y-3.5 pt-1 text-gray-700">
                    <div className="flex items-center space-x-2.5">
                      <GreenCheckIcon />
                      <span className="text-[13px] font-semibold text-[#1c1d22]">
                        {referredCustomers} {referredCustomers === 1 ? 'Customer' : 'Customers'} Referred.
                      </span>
                    </div>
                    <div className="flex items-center space-x-2.5">
                      <GreenCheckIcon />
                      <span className="text-[13px] font-semibold text-[#1c1d22]">
                        {referredVendors} {referredVendors === 1 ? 'Vendor' : 'Vendors'} Referred.
                      </span>
                    </div>
                    <div className="flex items-center space-x-2.5">
                      <GreenCheckIcon />
                      <span className="text-[13px] font-semibold text-[#1c1d22]">
                        {referredDrivers} {referredDrivers === 1 ? 'Driver' : 'Drivers'} Referred.
                      </span>
                    </div>
                  </div>
                </div>

                {/* Card Right Badge Display */}
                <div className="mt-6 md:mt-0 flex-shrink-0 flex items-center justify-center">
                  <MedalBadge level={currentLevel} locked={false} />
                </div>
              </div>
            )}
          </div>

          {/* Reward Earned Card Banner (Only shown if user has unlocked a level) */}
          {/* {currentLevel > 0 && (
            <div className="bg-gradient-to-r from-[#4a1d96] via-[#3b1a8c] to-[#1a066b] text-white rounded-2xl px-5 py-3.5 flex flex-row justify-between items-center shadow-sm">
              <div className="space-y-0.5">
                <p className="text-[10px] text-[#c4b5ff] font-semibold uppercase tracking-wider">Reward Earned</p>
                <p className="text-xl md:text-2xl font-bold tracking-tight text-white">${rewardAmount}</p>
              </div>
              <button
                onClick={() => router.push("/wallet")}
                className="bg-white hover:bg-gray-50 active:scale-95 text-[#1a066b] font-semibold px-4 py-2 rounded-lg text-xs md:text-sm transition-all duration-150 border-2 border-[#1a066b] cursor-pointer whitespace-nowrap"
              >
                View in Wallet
              </button>
            </div>
          )} */}

          {/* Next Certificate Level Section */}
          {nextLevelNum > 0 && (
            <div className="space-y-4 pt-2">
              <h2 className="text-[17px] font-bold text-gray-900 tracking-tight">
                {currentLevel === 0 ? "Your First Certificate Level" : "Next Certificate Level"}
              </h2>

              {/* White Container Card */}
              <div className="bg-white border border-gray-100 rounded-[24px] p-6 md:p-8 flex flex-col md:flex-row justify-between items-start relative overflow-hidden shadow-sm hover:shadow-md transition-all duration-300">
                {/* Next Level progress information */}
                <div className="flex-1 w-full space-y-8 text-left pr-0 md:pr-8">
                  <div>
                    <div className="flex items-center">
                      <p className="text-3xl font-extrabold text-[#1c1d22]">
                        Level {nextLevelNum}
                      </p>
                      <YellowLockIcon />
                    </div>
                    <p className="text-xs md:text-sm text-gray-400 mt-1.5 font-medium">
                      Unlock your {currentLevel === 0 ? "first" : "next"} Certificate Level by completing the following tasks.
                    </p>
                  </div>

                  {/* Progress bars list */}
                  <div className="space-y-5">
                    {/* Customers Referred Task */}
                    <div className="space-y-2.5">
                      <div className="flex justify-between items-center text-xs md:text-[13px] font-bold text-gray-800">
                        <span>Referred Customers</span>
                        <span className="text-[#1c1d22] font-semibold">{referredCustomers}/{targetCustomers}</span>
                      </div>
                      <div className="w-full bg-gray-100 rounded-full h-[7px] overflow-hidden">
                        <div
                          className="bg-[#1a066b] h-full rounded-full transition-all duration-500 ease-out"
                          style={{ width: `${customerProgress}%` }}
                        />
                      </div>
                    </div>

                    {/* Divider */}
                    <div className="border-t border-gray-100" />

                    {/* Vendors Referred Task */}
                    <div className="space-y-2.5">
                      <div className="flex justify-between items-center text-xs md:text-[13px] font-bold text-gray-800">
                        <span>Referred Vendors</span>
                        <span className="text-[#1c1d22] font-semibold">{referredVendors}/{targetVendors}</span>
                      </div>
                      <div className="w-full bg-gray-100 rounded-full h-[7px] overflow-hidden">
                        <div
                          className="bg-[#1a066b] h-full rounded-full transition-all duration-500 ease-out"
                          style={{ width: `${vendorProgress}%` }}
                        />
                      </div>
                    </div>

                    {/* Divider */}
                    <div className="border-t border-gray-100" />

                    {/* Drivers Referred Task */}
                    <div className="space-y-2.5">
                      <div className="flex justify-between items-center text-xs md:text-[13px] font-bold text-gray-800">
                        <span>Referred Drivers</span>
                        <span className="text-[#1c1d22] font-semibold">{referredDrivers}/{targetDrivers}</span>
                      </div>
                      <div className="w-full bg-gray-100 rounded-full h-[7px] overflow-hidden">
                        <div
                          className="bg-[#1a066b] h-full rounded-full transition-all duration-500 ease-out"
                          style={{ width: `${driverProgress}%` }}
                        />
                      </div>
                    </div>
                  </div>
                </div>

                {/* Locked Medal Display */}
                <div className="mt-6 md:mt-0 flex-shrink-0 flex items-center justify-center">
                  <MedalBadge level={nextLevelNum} locked={true} />
                </div>
              </div>
            </div>
          )}
        </div>
      )}
    </div>
  );
};

export default function RewardsPage() {
  return (
    <DashboardLayout>
      <Rewards />
    </DashboardLayout>
  );
}
